SocketClient.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace Common
  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. 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. //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint}");
  68. threadReceive = new Thread(Receive);
  69. threadReceive.IsBackground = true;
  70. threadReceive.Start();
  71. IsRun = true;
  72. }
  73. catch (Exception ex)
  74. {
  75. LogHelper.Info("平台插件未运行!请启动插件。");
  76. LogHelper.Info(ex.Message);
  77. LogHelper.Info(ex.StackTrace);
  78. }
  79. }
  80. /// <summary>
  81. /// 接收服务端发送过来的消息
  82. /// </summary>
  83. public void Receive()
  84. {
  85. //string message = "";
  86. try
  87. {
  88. byte[] TempData = new byte[1024 * 10];
  89. while (IsRun)
  90. {
  91. //传递一个byte数组,用于接收数据。length表示接收了多少字节的数据
  92. int length = _ClientSocket.Receive(TempData);
  93. if (length == 0)
  94. {
  95. IsRun = false;
  96. //Console.WriteLine("服务器断开链接");
  97. break;
  98. }
  99. else
  100. {
  101. Message = Encoding.Default.GetString(TempData, 0, length);//只将接收到的数据进行转化
  102. //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint} 获取到的数据:{message}");
  103. //Log.Info("出参:" + Message);
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. //Log.Info("95478");
  110. LogHelper.Info($"{ex.Message}");
  111. //Log.Info(ex.StackTrace, true);
  112. }
  113. }
  114. /// <summary>
  115. /// 发送消息到服务端
  116. /// </summary>
  117. /// <param name="data"></param>
  118. /// <returns></returns>
  119. public int Send(string data)
  120. {
  121. Message = null;
  122. return _ClientSocket.Send(Encoding.Default.GetBytes(data));
  123. }
  124. public void Close()
  125. {
  126. IsRun = false;
  127. if (threadReceive != null)
  128. {
  129. try
  130. {
  131. threadReceive.Interrupt();
  132. Thread.Sleep(200);
  133. }
  134. catch (Exception ex)
  135. {
  136. LogHelper.Info(ex.Message);
  137. LogHelper.Info(ex.StackTrace);
  138. }
  139. threadReceive = null;
  140. }
  141. if (_ClientSocket != null)
  142. {
  143. _ClientSocket.Close();
  144. }
  145. }
  146. }
  147. }