SocketClient.cs 4.8 KB

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