SocketClient.cs 4.7 KB

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