Asynchronous Socket Utility Classes – Part I - Sample Application 1 (Page 3 of 4 )
Now that we are done constructing our Socket Client class, we need to test how it works. The first thing to do is open the Class1.cs file. Add the SocketSystem namespace so we can access our new class CSocketClient.
using SocketSystem;
Now add the following static public functions to the Class1 class after the static Main function.
//********************************************** /// <summary> Called when a message is extracted from the socket </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> /// <param name="iNumberOfBytes"> The number of bytes in the RawBuffer inside the SocketClient </param> static public void MessageHandlerClient(CSocketClient pSocket, Int32 iNumberOfBytes) { try { // Convert the message from a byte array to a string String strMessage = System.Text.ASCIIEncoding.ASCII.GetString(pSocket.GetRawBuffer, 0, iNumberOfBytes);
// Display the string to the console window Console.WriteLine(strMessage); } catch (Exception pException) { Console.WriteLine(pException.Message); } } //********************************************* /// <summary> Called when a socket connection is closed </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> static public void CloseHandler(CSocketClient pSocket) { Console.WriteLine("Close Handler"); Console.WriteLine("IpAddress: " + pSocket.GetIpAddress); } //************************************************** /// <summary> Called when a socket error occurs </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> /// <param name="pException"> The reason for the error </param> static public void ErrorHandler(CSocketClient pSocket, Exception pException) { Console.WriteLine(pException.Message); }
Implement a function called TestClient. I thought it would be fun to use the CSocketClient class to connect to a web server and pretend our application was a browser. So what we will do is connect to the web server hosting www.continuumtechnologycenter.com, issue a HTTP Get command, and see what comes back through the Message Handler.
//********************************************* /// <summary> Function to test the CSocketClient class </summary> static void TestClient() { try { // Instantiate a CSocketClient object CSocketClient pSocketClient = new CSocketClient(10240, null, new CSocketClient.MESSAGE_HANDLER(MessageHandlerClient), new CSocketClient.CLOSE_HANDLER(CloseHandler), new CSocketClient.ERROR_HANDLER(ErrorHandler)); // Generate the HTTP Get command to send to the server String strBrowserRequest = "GET / HTTP/1.1\n" + "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, */*\n" + "Accept-Language: en-us\n" + "Accept-Encoding: gzip, deflate\n" + "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)\n" + "Host: www.continuumtechnologycenter.com:80\n" + "Connection: Keep-Alive\n\n"; // Establish a connection to the server hosting www.continuumtechnologycenter.com pSocketClient.Connect("www.continuumtechnologycenter.com", 80); // Send the HTTP Get command pSocketClient.Send(strBrowserRequest); Console.ReadLine(); pSocketClient.Disconnect(); Console.ReadLine(); pSocketClient.Dispose(); } catch (Exception pException) { Console.WriteLine(pException.Message); } }
Finallly add the following code to the Main function.
//************************************************* /// <summary> Function to test the CSocketClient class </summary> static void Main(string[] args) { // Test the CSocketClient class TestClient(); }
Build and run the application. You should get the following