SocketClient.cs 6.5 KB

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