Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Text;
- // State object for receiving data from remote device.
- public class StateObject
- {
- // Client socket.
- public Socket workSocket = null;
- // Size of receive buffer.
- public const int BufferSize = 256;
- // Receive buffer.
- public byte[] buffer = new byte[BufferSize];
- // Received data string.
- public StringBuilder sb = new StringBuilder();
- }
- public class AsynchronousClient
- {
- private const int port = 11000;
- private static ManualResetEvent connectDone =
- new ManualResetEvent(false);
- private static ManualResetEvent sendDone =
- new ManualResetEvent(false);
- private static ManualResetEvent receiveDone =
- new ManualResetEvent(false);
- private static String response = String.Empty;
- public static void StartClient(string message)
- {
- // Connect to a remote device.
- try
- {
- IPHostEntry ipHostInfo = Dns.GetHostEntry("my_address");
- IPAddress ipAddress = ipHostInfo.AddressList[0];
- IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
- Socket client = new Socket(ipAddress.AddressFamily,
- SocketType.Stream, ProtocolType.Tcp);
- client.BeginConnect(remoteEP,
- new AsyncCallback(ConnectCallback), client);
- connectDone.WaitOne();
- // Send test data to the remote device.
- //UnityEngine.Debug.Log("Client started...");
- Send(client, message + "<EOF>");
- sendDone.WaitOne();
- Receive(client);
- receiveDone.WaitOne();
- Console.WriteLine("Response received : {0}", response);
- client.Shutdown(SocketShutdown.Both);
- client.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void ConnectCallback(IAsyncResult ar)
- {
- try
- {
- Socket client = (Socket)ar.AsyncState;
- client.EndConnect(ar);
- Console.WriteLine("Socket connected to {0}",
- client.RemoteEndPoint.ToString());
- connectDone.Set();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void Receive(Socket client)
- {
- try
- {
- StateObject state = new StateObject();
- state.workSocket = client;
- client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
- new AsyncCallback(ReceiveCallback), state);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void ReceiveCallback(IAsyncResult ar)
- {
- try
- {
- StateObject state = (StateObject)ar.AsyncState;
- Socket client = state.workSocket;
- // Read data from the remote device.
- int bytesRead = client.EndReceive(ar);
- if (bytesRead > 0)
- {
- state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
- client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
- new AsyncCallback(ReceiveCallback), state);
- }
- else
- {
- if (state.sb.Length > 1)
- {
- response = state.sb.ToString();
- }
- receiveDone.Set();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void Send(Socket client, String data)
- {
- byte[] byteData = Encoding.ASCII.GetBytes(data);
- client.BeginSend(byteData, 0, byteData.Length, 0,
- new AsyncCallback(SendCallback), client);
- }
- private static void SendCallback(IAsyncResult ar)
- {
- try
- {
- Socket client = (Socket)ar.AsyncState;
- int bytesSent = client.EndSend(ar);
- Console.WriteLine("Sent {0} bytes to server.", bytesSent);
- sendDone.Set();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- public static void Main()
- {
- while (true)
- {
- string message = Console.ReadLine();
- StartClient(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement