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

Asynchronous Socket Utility Classes Part I

William has created a two part series on using Asynchronous Socket Utility Classes in C#. Part one will describe client side socket programming.

Author Info:
By: William Kennedy
Rating: 4 stars4 stars4 stars4 stars4 stars / 39
March 28, 2003
  1. · Asynchronous Socket Utility Classes Part I
  2. · Component Design and Coding
  3. · Sample Application 1
  4. · Review

print this article

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

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()
    // 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" +
" +
      "Connection: Keep-Alive\n\n";
    // Establish a connection to the server hosting
", 80);
    // Send the HTTP Get command
  catch (Exception pException)

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

Build and run the application.  You should get the following

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials