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  
Mobile Linux 
App Generation ROI 
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 II
By: William Kennedy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2003-03-31

    Table of Contents:
  • Asynchronous Socket Utility Classes – Part II
  • Component Design and Coding
  • Sample Application Update
  • 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 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.

    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 5 hosted by Hostway
    Stay green...Green IT