Home arrow C# arrow Page 3 - Asynchronous Socket Utility Classes Part II

Asynchronous Socket Utility Classes Part II

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.

Author Info:
By: William Kennedy
Rating: 5 stars5 stars5 stars5 stars5 stars / 22
March 31, 2003
  1. · Asynchronous Socket Utility Classes Part II
  2. · Component Design and Coding
  3. · Sample Application Update
  4. · Review

print this article

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)
    // Find a complete message
    String strMessage = System.Text.ASCIIEncoding.ASCII.GetString(pSocket.GetRawBuffer, 0, iNumberOfBytes);
    // 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";
  catch (Exception pException)

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()
    // 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   
  catch (Exception pException)

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

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.

Next: Review >>

blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials