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.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Test_Comm_Dist
- {
- // State object for data recovery from a remote device
- public class StateObject
- {
- // Socket Client
- public Socket workSocket = null;
- // Received Buffer size
- public const int BufferSize = 256;
- // Receive the buffer
- public byte[] buffer = new byte[BufferSize];
- // Receive data string
- public StringBuilder sb = new StringBuilder();
- }
- public class AsynchronousClient
- {
- // Port number for remote device
- private const int port = 11000;
- // ManualResetEvent instances report completion
- private static ManualResetEvent connectDone = new ManualResetEvent(false);
- private static ManualResetEvent sendDone = new ManualResetEvent(false);
- private static ManualResetEvent receiveDone = new ManualResetEvent(false);
- // Remote device response
- private static String response = String.Empty;
- private static void StartClient()
- {
- // Connects to the device remotely
- try
- {
- IPHostEntry ipHostInfo = Dns.GetHostEntry("192.168.56.1");
- IPAddress ipAddress = ipHostInfo.AddressList[0];
- IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
- // Create TCP/IP Socket
- Socket client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- // Connects to the remote endpoint
- client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
- connectDone.WaitOne();
- // Sends test data to the remote device
- Send(client, "This is a test<EOF>");
- sendDone.WaitOne();
- // Receive a response from the remote device
- Receive(client);
- receiveDone.WaitOne();
- // Writes the response to the console
- Console.WriteLine("Response received : {0}", response);
- // Release the socket
- client.Shutdown(SocketShutdown.Both);
- client.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void ConnectCallback(IAsyncResult ar)
- {
- try
- {
- // Get the socket of the state object
- Socket client = (Socket)ar.AsyncState;
- // End the connection
- client.EndConnect(ar);
- Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
- // Signal that the connection has been made
- connectDone.Set();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void Receive(Socket client)
- {
- try
- {
- // Creating the object state
- StateObject state = new StateObject();
- state.workSocket = client;
- // Starts receiving data from remote device
- 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
- {
- // Retrieve the state object and client socket from the asynchronous state object
- StateObject state = (StateObject)ar.AsyncState;
- Socket client = state.workSocket;
- // Read the data from the remote device
- int bytesRead = client.EndReceive(ar);
- if (bytesRead > 0)
- {
- // Allows to store data in addition
- state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
- // Gets the rest of the data
- client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback),
- state);
- }
- else
- {
- // Responding all data that has arrived
- if (state.sb.Length > 1)
- {
- response = state.sb.ToString();
- }
- // Signal that all bytes have been received
- receiveDone.Set();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- private static void Send(Socket client, String data)
- {
- // Convert string data to byte data using ASCII encoding
- byte[] byteData = Encoding.ASCII.GetBytes(data);
- // Start sending data to the remote device
- client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallBack), client);
- }
- private static void SendCallBack(IAsyncResult ar)
- {
- try
- {
- // Retrieves the sending of state object
- Socket client = (Socket)ar.AsyncState;
- // Finish sending data to the remote device
- int bytesSent = client.EndSend(ar);
- Console.WriteLine("Sent {0} bytes to server.", bytesSent);
- // Signal that all bytes have been sent
- sendDone.Set();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- public static int Main(String[] args)
- {
- StartClient();
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement