SocketLinker.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace SocketToolbox
  10. {
  11. class SocketLinker
  12. {
  13. public static SocketLinker ins = null;
  14. public static SocketHandler CurrentHandler = null;
  15. private Socket listener;
  16. private int listen_port = 9000;
  17. public void Start()
  18. {
  19. // Create a TCP/IP socket.
  20. listener = new Socket(AddressFamily.InterNetwork,
  21. SocketType.Stream, ProtocolType.Tcp);
  22. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), listen_port);
  23. // Bind the socket to the local endpoint and listen for incoming connections.
  24. listener.Bind(localEndPoint);
  25. listener.Listen(20);
  26. // Start an asynchronous socket to listen for connections.
  27. Console.WriteLine("Waiting for a connection...");
  28. listener.BeginAccept(
  29. new AsyncCallback(AcceptCallback),
  30. listener);
  31. }
  32. public void AcceptCallback(IAsyncResult ar)
  33. {
  34. try
  35. {
  36. // Get the socket that handles the client request.
  37. Socket listener = (Socket)ar.AsyncState;
  38. listener.BeginAccept(
  39. new AsyncCallback(AcceptCallback),
  40. listener);
  41. Socket conn = listener.EndAccept(ar);
  42. IPEndPoint remoteIPE = (IPEndPoint)(conn.RemoteEndPoint);
  43. // Create the state object.
  44. SocketHandler handler = new SocketHandler(0, new IPEndPoint(remoteIPE.Address, 7));
  45. handler.connection = conn;
  46. handler.StartReceive();
  47. CurrentHandler = handler;
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e.ToString());
  52. }
  53. }
  54. }
  55. class SocketHandler
  56. {
  57. //laser id
  58. public int id { get; set; }
  59. public bool writeAvailable { get; set; }
  60. public bool linkAvailable { get; set; }
  61. //remote ipe
  62. private EndPoint remotePE { get; set; }
  63. // Client socket.
  64. public Socket remote { get; set; }
  65. public Socket connection { get; set; }
  66. // Size of receive buffer.
  67. private const int BufferSize = 512;
  68. // remote receive buffer
  69. private byte[] sendBuffer = new byte[BufferSize];
  70. // connection receive buffer
  71. public byte[] receiveBuffer = new byte[BufferSize];
  72. public SocketHandler(int id, EndPoint pe)
  73. {
  74. this.id = id;
  75. remotePE = pe;
  76. }
  77. public void StartReceive()
  78. {
  79. try
  80. {
  81. Receive();
  82. remote = new Socket(remotePE.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  83. // Connect to the remote endpoint.
  84. remote.BeginConnect(remotePE, new AsyncCallback(ConnectCallback), null);
  85. }
  86. catch (Exception e)
  87. {
  88. Console.WriteLine(e.ToString());
  89. this.Close();
  90. }
  91. }
  92. public void StartSend()
  93. {
  94. try
  95. {
  96. remote = new Socket(remotePE.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  97. // Connect to the remote endpoint.
  98. remote.BeginConnect(remotePE, new AsyncCallback(ConnectCallback), null);
  99. }
  100. catch (Exception e)
  101. {
  102. Console.WriteLine(e.ToString());
  103. this.Close();
  104. }
  105. }
  106. public void Close()
  107. {
  108. if (connection != null)
  109. {
  110. connection.Close();
  111. }
  112. if (remote != null)
  113. {
  114. remote.Close();
  115. }
  116. }
  117. private void ConnectCallback(IAsyncResult ar)
  118. {
  119. try
  120. {
  121. // Complete the connection.
  122. remote.EndConnect(ar);
  123. Console.WriteLine("Socket connected to {0}", remote.RemoteEndPoint.ToString());
  124. writeAvailable = true;
  125. }
  126. catch (Exception e)
  127. {
  128. Console.WriteLine(e.ToString());
  129. this.Close();
  130. writeAvailable = false;
  131. }
  132. }
  133. private void Receive()
  134. {
  135. try
  136. {
  137. connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null);
  138. }
  139. catch (Exception e)
  140. {
  141. Console.WriteLine(e.ToString());
  142. this.Close();
  143. linkAvailable = false;
  144. }
  145. }
  146. private void Send()
  147. {
  148. receiveBuffer.CopyTo(sendBuffer, 0);
  149. try
  150. {
  151. remote.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), null);
  152. }
  153. catch (Exception e)
  154. {
  155. Console.WriteLine(e.ToString());
  156. this.Close();
  157. writeAvailable = false;
  158. }
  159. }
  160. public void Send(byte[] bs)
  161. {
  162. bs.CopyTo(sendBuffer, 0);
  163. try
  164. {
  165. remote.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), null);
  166. }
  167. catch (Exception e)
  168. {
  169. Console.WriteLine(e.ToString());
  170. this.Close();
  171. writeAvailable = false;
  172. }
  173. }
  174. private void ReceiveCallback(IAsyncResult ar)
  175. {
  176. try
  177. {
  178. int bytesRead = connection.EndReceive(ar);
  179. //获得信息则再次监听
  180. if (bytesRead > 0)
  181. {
  182. //Send();
  183. connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null);
  184. }
  185. else
  186. {
  187. this.Close();
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. Console.WriteLine("雷达" + id + "掉线\n" + e.ToString());
  193. this.Close();
  194. linkAvailable = false;
  195. }
  196. }
  197. private void SendCallback(IAsyncResult ar)
  198. {
  199. try
  200. {
  201. remote.EndSend(ar);
  202. }
  203. catch (Exception e)
  204. {
  205. Console.WriteLine(e.ToString());
  206. this.Close();
  207. writeAvailable = false;
  208. }
  209. }
  210. }
  211. }