SocketClient.cs 7.1 KB

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