SocketClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /// <param name="data"></param>
  126. /// <returns></returns>
  127. public int Send(string data)
  128. {
  129. Message = null;
  130. return _ClientSocket.Send(Encoding.Default.GetBytes(data));
  131. }
  132. public void Close()
  133. {
  134. IsRun = false;
  135. if (threadReceive != null)
  136. {
  137. try
  138. {
  139. threadReceive.Interrupt();
  140. Thread.Sleep(200);
  141. }
  142. catch (Exception ex)
  143. {
  144. Log.Info(ex.Message, true);
  145. Log.Info(ex.StackTrace, true);
  146. }
  147. threadReceive = null;
  148. }
  149. if (_ClientSocket != null)
  150. {
  151. _ClientSocket.Close();
  152. }
  153. }
  154. }
  155. }