using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace Emergency_platform { public class AlgorithmTCP { static Socket socket_commu; public static string Connect(string picPath, string targetname) { //IP地址192.168.1.32 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress serverAddress = IPAddress.Parse("192.168.10.104"); int port = 20000; client.Connect(new IPEndPoint(serverAddress, port)); Console.WriteLine("连接成功"); //发送图片地址和识别目标名字 var json = new { picadress = picPath, targetname = targetname }; //将JSON数据对象序列化为字符串 string jsonString = JsonConvert.SerializeObject(json); //将JSON字符串转换为字节数组 byte[] dataBytes = Encoding.UTF8.GetBytes(jsonString); socket_commu.Send(dataBytes); //等待算法识别结果 //接收数据 byte[] buffer = new byte[1024 * 1024]; int bytesRead = socket_commu.Receive(buffer); //将接收到的数据转换为字符串 string result = Encoding.UTF8.GetString(buffer, 0, bytesRead); return result; } } }