C#
  Home arrow C# arrow Page 3 - Asynchronous Socket Utility Classes – Part...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Asynchronous Socket Utility Classes – Part I
By: William Kennedy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2003-03-28

    Table of Contents:
  • Asynchronous Socket Utility Classes – Part I
  • Component Design and Coding
  • Sample Application 1
  • Review

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More C# Articles
    More By William Kennedy


     

    C# ARTICLES

    - 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#







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway