Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Test_Comm_Dist
  11. {
  12. // State object for data recovery from a remote device
  13. public class StateObject
  14. {
  15. // Socket Client
  16. public Socket workSocket = null;
  17. // Received Buffer size
  18. public const int BufferSize = 256;
  19. // Receive the buffer
  20. public byte[] buffer = new byte[BufferSize];
  21. // Receive data string
  22. public StringBuilder sb = new StringBuilder();
  23.  
  24. }
  25.  
  26. public class AsynchronousClient
  27. {
  28. // Port number for remote device
  29. private const int port = 11000;
  30.  
  31. // ManualResetEvent instances report completion
  32. private static ManualResetEvent connectDone = new ManualResetEvent(false);
  33. private static ManualResetEvent sendDone = new ManualResetEvent(false);
  34. private static ManualResetEvent receiveDone = new ManualResetEvent(false);
  35.  
  36. // Remote device response
  37. private static String response = String.Empty;
  38.  
  39. private static void StartClient()
  40. {
  41. // Connects to the device remotely
  42. try
  43. {
  44. IPHostEntry ipHostInfo = Dns.GetHostEntry("192.168.56.1");
  45. IPAddress ipAddress = ipHostInfo.AddressList[0];
  46. IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
  47.  
  48. // Create TCP/IP Socket
  49. Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  50.  
  51. // Connects to the remote endpoint
  52. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
  53. connectDone.WaitOne();
  54.  
  55. // Sends test data to the remote device
  56. Send(client, "This is a test<EOF>");
  57. sendDone.WaitOne();
  58.  
  59. // Receive a response from the remote device
  60. Receive(client);
  61. receiveDone.WaitOne();
  62.  
  63. // Writes the response to the console
  64. Console.WriteLine("Response received : {0}", response);
  65.  
  66. // Release the socket
  67. client.Shutdown(SocketShutdown.Both);
  68. client.Close();
  69. }
  70. catch (Exception e)
  71. {
  72. Console.WriteLine(e.ToString());
  73. }
  74. }
  75.  
  76. private static void ConnectCallback(IAsyncResult ar)
  77. {
  78. try
  79. {
  80. // Get the socket of the state object
  81. Socket client = (Socket)ar.AsyncState;
  82.  
  83. // End the connection
  84. client.EndConnect(ar);
  85.  
  86. Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
  87.  
  88. // Signal that the connection has been made
  89. connectDone.Set();
  90. }
  91. catch (Exception e)
  92. {
  93. Console.WriteLine(e.ToString());
  94. }
  95. }
  96. private static void Receive(Socket client)
  97. {
  98. try
  99. {
  100. // Creating the object state
  101. StateObject state = new StateObject();
  102. state.workSocket = client;
  103.  
  104. // Starts receiving data from remote device
  105. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback),
  106. state);
  107. }
  108. catch (Exception e)
  109. {
  110. Console.WriteLine(e.ToString());
  111.  
  112. }
  113. }
  114.  
  115. private static void ReceiveCallback(IAsyncResult ar)
  116. {
  117. try
  118. {
  119. // Retrieve the state object and client socket from the asynchronous state object
  120. StateObject state = (StateObject)ar.AsyncState;
  121. Socket client = state.workSocket;
  122.  
  123. // Read the data from the remote device
  124. int bytesRead = client.EndReceive(ar);
  125.  
  126. if (bytesRead > 0)
  127. {
  128. // Allows to store data in addition
  129. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
  130.  
  131. // Gets the rest of the data
  132. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback),
  133. state);
  134. }
  135. else
  136. {
  137. // Responding all data that has arrived
  138. if (state.sb.Length > 1)
  139. {
  140. response = state.sb.ToString();
  141. }
  142. // Signal that all bytes have been received
  143. receiveDone.Set();
  144. }
  145. }
  146.  
  147. catch (Exception e)
  148. {
  149. Console.WriteLine(e.ToString());
  150. }
  151. }
  152.  
  153. private static void Send(Socket client, String data)
  154. {
  155. // Convert string data to byte data using ASCII encoding
  156. byte[] byteData = Encoding.ASCII.GetBytes(data);
  157.  
  158. // Start sending data to the remote device
  159. client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), client);
  160. }
  161.  
  162. private static void SendCallBack(IAsyncResult ar)
  163. {
  164. try
  165. {
  166. // Retrieves the sending of state object
  167. Socket client = (Socket)ar.AsyncState;
  168.  
  169. // Finish sending data to the remote device
  170. int bytesSent = client.EndSend(ar);
  171. Console.WriteLine("Sent {0} bytes to server.", bytesSent);
  172.  
  173. // Signal that all bytes have been sent
  174. sendDone.Set();
  175. }
  176. catch (Exception e)
  177. {
  178. Console.WriteLine(e.ToString());
  179. }
  180. }
  181.  
  182. public static int Main(String[] args)
  183. {
  184. StartClient();
  185. return 0;
  186. }
  187. }
  188.  
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement