Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Text;
  6.  
  7. // State object for receiving data from remote device.  
  8. public class StateObject
  9. {
  10.     // Client socket.  
  11.     public Socket workSocket = null;
  12.     // Size of receive buffer.  
  13.     public const int BufferSize = 256;
  14.     // Receive buffer.  
  15.     public byte[] buffer = new byte[BufferSize];
  16.     // Received data string.  
  17.     public StringBuilder sb = new StringBuilder();
  18. }
  19.  
  20. public class AsynchronousClient
  21. {
  22.     private const int port = 11000;
  23.  
  24.     private static ManualResetEvent connectDone =
  25.         new ManualResetEvent(false);
  26.     private static ManualResetEvent sendDone =
  27.         new ManualResetEvent(false);
  28.     private static ManualResetEvent receiveDone =
  29.         new ManualResetEvent(false);
  30.  
  31.     private static String response = String.Empty;
  32.  
  33.     public static void StartClient(string message)
  34.     {
  35.         // Connect to a remote device.  
  36.         try
  37.         {
  38.             IPHostEntry ipHostInfo = Dns.GetHostEntry("my_address");
  39.             IPAddress ipAddress = ipHostInfo.AddressList[0];
  40.             IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
  41.  
  42.             Socket client = new Socket(ipAddress.AddressFamily,
  43.                 SocketType.Stream, ProtocolType.Tcp);
  44.            
  45.             client.BeginConnect(remoteEP,
  46.                 new AsyncCallback(ConnectCallback), client);
  47.             connectDone.WaitOne();
  48.  
  49.             // Send test data to the remote device.  
  50.             //UnityEngine.Debug.Log("Client started...");
  51.             Send(client, message + "<EOF>");
  52.  
  53.             sendDone.WaitOne();
  54.  
  55.             Receive(client);
  56.             receiveDone.WaitOne();
  57.  
  58.             Console.WriteLine("Response received : {0}", response);
  59.  
  60.             client.Shutdown(SocketShutdown.Both);
  61.             client.Close();
  62.  
  63.         }
  64.         catch (Exception e)
  65.         {
  66.             Console.WriteLine(e.ToString());
  67.         }
  68.     }
  69.  
  70.     private static void ConnectCallback(IAsyncResult ar)
  71.     {
  72.         try
  73.         {
  74.             Socket client = (Socket)ar.AsyncState;
  75.  
  76.             client.EndConnect(ar);
  77.  
  78.             Console.WriteLine("Socket connected to {0}",
  79.                 client.RemoteEndPoint.ToString());
  80.  
  81.             connectDone.Set();
  82.         }
  83.         catch (Exception e)
  84.         {
  85.             Console.WriteLine(e.ToString());
  86.         }
  87.     }
  88.  
  89.     private static void Receive(Socket client)
  90.     {
  91.         try
  92.         {
  93.             StateObject state = new StateObject();
  94.             state.workSocket = client;
  95.  
  96.             client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
  97.                 new AsyncCallback(ReceiveCallback), state);
  98.         }
  99.         catch (Exception e)
  100.         {
  101.             Console.WriteLine(e.ToString());
  102.         }
  103.     }
  104.  
  105.     private static void ReceiveCallback(IAsyncResult ar)
  106.     {
  107.         try
  108.         {
  109.  
  110.             StateObject state = (StateObject)ar.AsyncState;
  111.             Socket client = state.workSocket;
  112.  
  113.             // Read data from the remote device.  
  114.             int bytesRead = client.EndReceive(ar);
  115.  
  116.             if (bytesRead > 0)
  117.             {
  118.                 state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
  119.  
  120.                 client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
  121.                     new AsyncCallback(ReceiveCallback), state);
  122.             }
  123.             else
  124.             {
  125.  
  126.                 if (state.sb.Length > 1)
  127.                 {
  128.                     response = state.sb.ToString();
  129.                 }
  130.  
  131.                 receiveDone.Set();
  132.             }
  133.         }
  134.         catch (Exception e)
  135.         {
  136.             Console.WriteLine(e.ToString());
  137.         }
  138.     }
  139.  
  140.     private static void Send(Socket client, String data)
  141.     {
  142.  
  143.         byte[] byteData = Encoding.ASCII.GetBytes(data);
  144.  
  145.         client.BeginSend(byteData, 0, byteData.Length, 0,
  146.             new AsyncCallback(SendCallback), client);
  147.     }
  148.  
  149.     private static void SendCallback(IAsyncResult ar)
  150.     {
  151.         try
  152.         {
  153.  
  154.             Socket client = (Socket)ar.AsyncState;
  155.  
  156.  
  157.             int bytesSent = client.EndSend(ar);
  158.             Console.WriteLine("Sent {0} bytes to server.", bytesSent);
  159.  
  160.             sendDone.Set();
  161.         }
  162.         catch (Exception e)
  163.         {
  164.             Console.WriteLine(e.ToString());
  165.         }
  166.     }
  167.  
  168.     public static void Main()
  169.     {
  170.         while (true)
  171.         {
  172.             string message = Console.ReadLine();
  173.             StartClient(message);
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement