This is part two of William's series on using Asynchronous Socket Utility Classes in C#. In this article, William will describe the code for the server side. Once you have read the two parts you should have no troubles with these classes.
Asynchronous Socket Utility Classes – Part II - Sample Application Update (Page 3 of 4 )
Now it is time to test our CSocketServer class. Open the class1.cs file and go to the bottom. Add a new method called MessageHandlerServer. This method is called when we receive a message from a socket client connection. In this method we will send back to the client a HTTP HTML command.
//******************************************** /// <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 MessageHandlerServer(CSocketClient pSocket, Int32 iNumberOfBytes) { try { // Find a complete message String strMessage = System.Text.ASCIIEncoding.ASCII.GetString(pSocket.GetRawBuffer, 0, iNumberOfBytes); Console.WriteLine(strMessage); // Send the following HTTP command back String strServerResponse = "HTTP/1.1 200 OK\n" + "Date: Tue, 18 Feb 2003 18:47:39 GMT\n" + "Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) mod_perl/1.24_01 PHP/4.2.2 FrontPage/5.0.2 mod_ssl/2.8.12 OpenSSL/0.9.6b\n" + "Last-Modified: Sat, 25 Jan 2003 21:45:30 GMT\n" + "ETag: \"2201ab-11cd-3e33057a\"\n" + "Accept-Ranges: bytes\n" + "Content-Length: 53\n" + "Keep-Alive: timeout=15, max=100\n" + "Connection: Keep-Alive\n" + "Content-Type: text/html\n\n" + "<html> <body> <h1> Hello World! </h1> </body> </html>\n"; pSocket.Send(strServerResponse); } catch (Exception pException) { Console.WriteLine(pException.Message); } }
Add the AcceptHandler next. This is allow us to be notified when a connection is established to our server.
//******************************************** /// <summary> Called when a socket connection is accepted </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> static public void AcceptHandler(CSocketClient pSocket) { Console.WriteLine("Accept Handler"); Console.WriteLine("IpAddress: " + pSocket.GetIpAddress); }
The TestServer method will be used to start the server. We will listen for connections on port 9000 using the network card configured under our machine name.
//********************************************* /// <summary> Function to test the CSocketServer class </summary> static void TestServer() { try { // Instantiate a CSocketServer object CSocketServer pSocketServer = new CSocketServer(); // Start listening for connections pSocketServer.Start(System.Environment.MachineName, 9000, 1024, 10240, null, new CSocketServer.MESSAGE_HANDLER(MessageHandlerServer), new CSocketServer.ACCEPT_HANDLER(AcceptHandler), new CSocketServer.CLOSE_HANDLER(CloseHandler), new CSocketServer.ERROR_HANDLER(ErrorHandler)); Console.WriteLine("Waiting for a client connection on Machine: {0} Port: {1}", System.Environment.MachineName, 9000); // Stay here until you are ready to shutdown the server Console.ReadLine(); pSocketServer.Dispose(); } catch (Exception pException) { Console.WriteLine(pException.Message); } }
In the Main method, comment out the call to TestClient and add the call to TestServer. Now we are ready to start our server and test it.
//********************************************** /// <summary> Function to test the CSocketClient class </summary> static void Main(string[] args) { // Test the CSocketClient class // TestClient();
// Test the CSocketClient class TestServer(); }
Start the server and launch your browser. Our server is listening for connections on our local machine on port 9000. You need the machine name or ipaddress of our local machine. In the address bar type the following: http://machinename:9000 . This will have the browser connect to our server on port 9000 and our server will send the browser back the HTML statement that will display Hello World on the browser.