1 private static byte[] result = new byte[1024]; 2 /// <summary> 3 /// HL7串通过socket发送到VP 4 /// </summary> 5 /// <param name="msg"></param> 6 /// <returns></returns> 7 public static string sendhl7(string msg, int sendingTimes) 8 { 9 10 while (sendingTimes < 3) 11 { 12 Thread.Sleep(100); //等待0.1秒钟 13 sendingTimes++; 14 var socketIP = System.Configuration.ConfigurationManager.AppSettings["socketIP"]; 15 var socketPort = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["socketPort"]); 16 //设定服务器IP地址 17 IPAddress ip = IPAddress.Parse(socketIP); 18 Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 19 int error = 0; 20 try 21 { 22 clientSocket.Connect(new IPEndPoint(ip, socketPort)); //配置服务器IP与端口 23 error++; 24 clientSocket.Send(Encoding.UTF8.GetBytes(msg)); 25 int receiveL = clientSocket.Receive(result); 26 LogManager.WriteLog("第" + sendingTimes + "次连接VP的socket服务发送消息成功", Encoding.UTF8.GetString(result, 0, receiveL)); 27 clientSocket.Close(); 28 return Encoding.UTF8.GetString(result, 0, receiveL); 29 } 30 31 catch (Exception ex) 32 { 33 LogManager.WriteLog("第" + sendingTimes + "次给VP的socket服务传消息报错", ex.ToString()); 34 if (error == 0) 35 { 36 LogManager.WriteLog("连接VP的socket服务", " 第" + sendingTimes + "次连接失败! 连接信息 " + msg); 37 } 38 if (error == 1) 39 { 40 LogManager.WriteLog("给VP的socket服务 第" + sendingTimes + "次发送消息报错", "连接信息: " + msg); 41 } 42 if (clientSocket != null && clientSocket.Connected) 43 { 44 clientSocket.Shutdown(SocketShutdown.Both); 45 clientSocket.Close(); 46 } 47 continue; 48 } 49 } 50 return ""; 51 }