SocketTcpServer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace AGV_WPF.Services
  8. {
  9. public class SocketTcpServer
  10. {
  11. private Socket socket;
  12. private IPEndPoint IpEndPoint;
  13. private bool ListenerBool = false;
  14. public Dictionary<string, Socket> SocketDic = new Dictionary<string, Socket>();
  15. public delegate byte[] DealMessage(Socket SocketClient, byte[] data);
  16. public DealMessage ReceiveDataDeal { get; set; }
  17. public SocketTcpServer(int Port, string IP = null)
  18. {
  19. if (IP == null)
  20. {
  21. IpEndPoint = new IPEndPoint(IPAddress.Any, Port);
  22. }
  23. else
  24. {
  25. IpEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port);
  26. }
  27. }
  28. private bool IsConnected(Socket SocketClient)
  29. {
  30. if (SocketClient == null) return false;
  31. if (!SocketClient.Connected) return false;
  32. //if (!SocketClient.Poll(1000, SelectMode.SelectRead)) return false;
  33. if (!SocketClient.Poll(50, SelectMode.SelectWrite)) return false;
  34. if (SocketClient.Poll(50, SelectMode.SelectError)) return false;
  35. return true;
  36. }
  37. private void ListenerClient(int ListenNumber, int TimeOut)
  38. {
  39. while (ListenerBool)
  40. {
  41. Socket connectClient = socket.Accept();
  42. if (IsConnected(connectClient))
  43. {
  44. Task.Factory.StartNew(() => ReceiveMessage(connectClient, TimeOut));
  45. if (!SocketDic.ContainsKey(connectClient.RemoteEndPoint.ToString()))
  46. {
  47. SocketDic.Add(connectClient.RemoteEndPoint.ToString(), connectClient);
  48. }
  49. }
  50. Thread.Sleep(200);
  51. }
  52. }
  53. private void ReceiveMessage(Socket SocketClient, int TimeOut, bool ConnectionBool = true)
  54. {
  55. DateTime timer = DateTime.Now; EndPoint RemoteEndPoint = SocketClient.RemoteEndPoint;
  56. while (ConnectionBool && IsConnected(SocketClient))
  57. {
  58. try
  59. {
  60. if (!SocketDic.ContainsKey(RemoteEndPoint.ToString())) { Thread.Sleep(500); continue; }
  61. byte[] Data = new byte[1024];
  62. int Length = SocketClient.Receive(Data, 0, Data.Length, SocketFlags.None);
  63. if (Length > 0)
  64. {
  65. byte[] receiveData = new byte[Length];
  66. for (int i = 0; i < Length; i++) { receiveData[i] = Data[i]; }
  67. if (ReceiveDataDeal != null)
  68. {
  69. byte[] sendData = ReceiveDataDeal.Invoke(SocketClient, receiveData);
  70. if (sendData != null && sendData.Length > 0) { SendMessage(SocketClient, sendData); }
  71. timer = DateTime.Now;
  72. }
  73. }
  74. else if (Length == 0)
  75. {
  76. ConnectionBool = false; if (SocketClient != null) { SocketClient.Close(); }
  77. SocketDic.Remove(RemoteEndPoint.ToString());
  78. }
  79. if ((DateTime.Now - timer).TotalMinutes > TimeOut)
  80. {
  81. ConnectionBool = false; if (SocketClient != null) { SocketClient.Close(); }
  82. SocketDic.Remove(RemoteEndPoint.ToString());
  83. }
  84. Thread.Sleep(200);
  85. }
  86. catch (Exception ex)
  87. {
  88. ConnectionBool = false; if (SocketClient != null) { SocketClient.Close(); }
  89. SocketDic.Remove(RemoteEndPoint.ToString());
  90. }
  91. }
  92. }
  93. public bool SendMessage(Socket SocketClient, byte[] Data)
  94. {
  95. if (IsConnected(SocketClient) && Data.Length > 0)
  96. {
  97. return SocketClient.Send(Data, 0, Data.Length, SocketFlags.None) > 0;
  98. }
  99. return false;
  100. }
  101. public bool StartListener(int ListenNumber, int TimeOut)
  102. {
  103. try
  104. {
  105. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  106. socket.Bind(IpEndPoint); ListenerBool = true; socket.Listen(ListenNumber);
  107. Task.Factory.StartNew(() => ListenerClient(ListenNumber, TimeOut));
  108. return true;
  109. }
  110. catch
  111. {
  112. return false;
  113. }
  114. }
  115. public void CloseListener()
  116. {
  117. foreach (var item in SocketDic) { item.Value.Close(); }
  118. SocketDic.Clear(); ListenerBool = false; if (socket != null) { socket.Close(); }
  119. }
  120. public bool CloseClientConnection(Socket SocketClient)
  121. {
  122. if (IsConnected(SocketClient))
  123. {
  124. if (SocketDic.ContainsKey(SocketClient.RemoteEndPoint.ToString()))
  125. {
  126. SocketDic.Remove(SocketClient.RemoteEndPoint.ToString());
  127. }
  128. SocketClient.Close();
  129. return true;
  130. }
  131. return false;
  132. }
  133. }
  134. }