SocketClient.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Security;
  5. using System.Text;
  6. using System.Threading;
  7. using ZLPlugin_LisPacs_MR.Domain.Units;
  8. namespace ZLPlugin_LisPacs_MR
  9. {
  10. /// <summary>
  11. /// socket客户端
  12. /// </summary>
  13. public class SocketClient
  14. {
  15. public static string Message = null;//接收消息
  16. /// <summary>
  17. /// 客户端socket
  18. /// </summary>
  19. public Socket _ClientSocket = null;
  20. /// <summary>
  21. /// IP地址
  22. /// </summary>
  23. public string ipAddress = string.Empty;
  24. /// <summary>
  25. /// IP端口
  26. /// </summary>
  27. public int Port;
  28. /// <summary>
  29. /// 接收线程
  30. /// </summary>
  31. private Thread threadReceive;
  32. /// <summary>
  33. /// 是否运行
  34. /// </summary>
  35. private bool IsRun = false;
  36. /// <summary>
  37. /// 远端地址
  38. /// </summary>
  39. private string remoteEndPoint;
  40. /// <summary>
  41. /// 本端地址
  42. /// </summary>
  43. private string LocalEndPoint;
  44. /// <summary>
  45. /// 客户端
  46. /// </summary>
  47. /// <param name="ip"></param>
  48. /// <param name="Port"></param>
  49. public SocketClient(string ip,int Port)
  50. {
  51. _ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  52. _ClientSocket.ReceiveTimeout = 60000;
  53. this.ipAddress = ip;
  54. this.Port = Port;
  55. Start();
  56. }
  57. /// <summary>
  58. /// 启动连接服务端
  59. /// </summary>
  60. public void Start()
  61. {
  62. try
  63. {
  64. _ClientSocket.Connect(new IPEndPoint(IPAddress.Parse(this.ipAddress), this.Port));//通过IP和端口号来定位一个所要连接的服务器端
  65. //客户端网络结点号
  66. remoteEndPoint = _ClientSocket.RemoteEndPoint.ToString();
  67. LocalEndPoint = _ClientSocket.LocalEndPoint.ToString();
  68. Log.Info($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint}");
  69. //threadReceive = new Thread(Receive);
  70. //threadReceive.IsBackground = true;
  71. //threadReceive.Start();
  72. /* remoteEndPoint = _ClientSocket.RemoteEndPoint.ToString();
  73. LocalEndPoint = _ClientSocket.LocalEndPoint.ToString();
  74. //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint}");
  75. threadReceive = new Thread(Receive);
  76. threadReceive.IsBackground = true;
  77. threadReceive.Start();*/
  78. IsRun = true;
  79. }
  80. catch (Exception ex)
  81. {
  82. Log.Info("平台插件未运行!请启动插件。", true);
  83. Log.Info(ex.Message, true);
  84. Log.Info(ex.StackTrace, true);
  85. }
  86. }
  87. /// <summary>
  88. /// 接收服务端发送过来的消息
  89. /// </summary>
  90. public void Receive()
  91. {
  92. //string message = "";
  93. try
  94. {
  95. byte[] TempData = new byte[1024 * 10];
  96. while (IsRun)
  97. {
  98. //传递一个byte数组,用于接收数据。length表示接收了多少字节的数据
  99. int length = _ClientSocket.Receive(TempData);
  100. if (length == 0)
  101. {
  102. IsRun = false;
  103. //Console.WriteLine("服务器断开链接");
  104. break;
  105. }
  106. else
  107. {
  108. Message = Encoding.Default.GetString(TempData, 0, length);//只将接收到的数据进行转化
  109. //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint} 获取到的数据:{message}");
  110. //Log.Info("出参:" + Message);
  111. }
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. //Log.Info("95478");
  117. Log.Info(ex.Message);
  118. //Log.Info(ex.StackTrace, true);
  119. }
  120. }
  121. /// <summary>
  122. /// 循环接收消息 直到有数据
  123. /// </summary>
  124. public void WhileReceive(int waittingdate)
  125. {
  126. byte[] buffer = new byte[1024*100];
  127. int receivedBytes = 0;
  128. var begintime = DateTime.Now.Ticks;
  129. TimeSpan timeSpan = new TimeSpan();
  130. bool flag = true;
  131. Log.Info("WhileReceive 开始接收消息》》");
  132. while (flag)
  133. {
  134. Log.Info("flag=" + flag);
  135. // 只循环 20 秒
  136. timeSpan = new TimeSpan(DateTime.Now.Ticks - begintime);
  137. Log.Info("timeSpan=" + timeSpan);
  138. if (timeSpan.TotalSeconds > waittingdate)
  139. {
  140. flag = false;
  141. Log.Info("flag=" + flag);
  142. }
  143. while (flag)
  144. {
  145. try
  146. {
  147. //传递一个byte数组,用于接收数据。length表示接收了多少字节的数据
  148. receivedBytes = _ClientSocket.Receive(buffer);
  149. }catch (ArgumentNullException e)
  150. {
  151. Log.Info("获取数据超时 ArgumentNullException " + e.Message);
  152. break;
  153. }
  154. catch (SocketException e)
  155. {
  156. Log.Info("获取数据超时 SocketException" + e.Message);
  157. break;
  158. }
  159. catch (ObjectDisposedException e)
  160. {
  161. Log.Info("获取数据超时 ObjectDisposedException" + e.Message);
  162. break;
  163. }
  164. catch (SecurityException e)
  165. {
  166. Log.Info("获取数据超时 SecurityException" + e.Message);
  167. break;
  168. }
  169. }
  170. Log.Info("receivedBytes " + receivedBytes);
  171. if (receivedBytes > 0)
  172. {
  173. Message = Encoding.Default.GetString(buffer, 0, receivedBytes);//只将接收到的数据进行转化
  174. Log.Info("WhileReceive 返回消息》》"+Message);
  175. break;
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// 发送消息到服务端
  181. /// </summary>
  182. /// <param name="data"></param>
  183. /// <returns></returns>
  184. public int Send(string data)
  185. {
  186. Message = null;
  187. return _ClientSocket.Send(Encoding.Default.GetBytes(data));
  188. }
  189. public void Close()
  190. {
  191. IsRun = false;
  192. if (threadReceive != null)
  193. {
  194. try
  195. {
  196. threadReceive.Interrupt();
  197. Thread.Sleep(200);
  198. }
  199. catch (Exception ex)
  200. {
  201. Log.Info(ex.Message, true);
  202. Log.Info(ex.StackTrace, true);
  203. }
  204. threadReceive = null;
  205. }
  206. if (_ClientSocket != null)
  207. {
  208. _ClientSocket.Close();
  209. }
  210. }
  211. }
  212. }