Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 27,908 times

Contents

Downloads

Related Categories

Create your own Web Server using C# - The Code (2)

Imtiaz Alam

The Code (2)

We also need to identify the Mime type, using the file extension supplied by the user

public string GetMimeType(string sRequestedFile)
{


    StreamReader sr;
    String sLine = "";
    String sMimeType = "";
    String sFileExt = "";
    String sMimeExt = "";

    // Convert to lowercase
    sRequestedFile = sRequestedFile.ToLower();

    int iStartPos = sRequestedFile.IndexOf(".");

    sFileExt = sRequestedFile.Substring(iStartPos);

    try
    {
        //Open the Vdirs.dat to find out the list virtual directories
        sr = new StreamReader("data\\Mime.Dat");

        while ((sLine = sr.ReadLine()) != null)
        {

            sLine.Trim();

            if (sLine.Length > 0)
            {
                //find the separator
                iStartPos = sLine.IndexOf(";");

                // Convert to lower case
                sLine = sLine.ToLower();

                sMimeExt = sLine.Substring(0,iStartPos);
                sMimeType = sLine.Substring(iStartPos + 1);

                if (sMimeExt == sFileExt)
                    break;
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("An Exception Occurred : " + e.ToString());
    }

    if (sMimeExt == sFileExt)
        return sMimeType; 
    else
        return "";
}

Now we will write the function, to build and sends header information to the browser (client)

public void SendHeader(string sHttpVersion, string sMIMEHeader,
    int iTotBytes, string sStatusCode, ref Socket mySocket)
{

    String sBuffer = "";
    
    // if Mime type is not provided set default to text/html
    if (sMIMEHeader.Length == 0 )
    {
        sMIMEHeader = "text/html";  // Default Mime Type is text/html
    }

    sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
    sBuffer = sBuffer + "Server: cx1193719-b\r\n";
    sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";
    sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
    sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n";
    
    Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer); 

    SendToBrowser( bSendData, ref mySocket);

    Console.WriteLine("Total Bytes : " + iTotBytes.ToString());

}

The SendToBrowser function sends information to the browser. This is an overloaded function.


public void SendToBrowser(String sData, ref Socket mySocket)
{
    SendToBrowser (Encoding.ASCII.GetBytes(sData), ref mySocket);
}


public void SendToBrowser(Byte[] bSendData, ref Socket mySocket)
{
    int numBytes = 0;
    try
    {
        if (mySocket.Connected)
        {
            if (( numBytes = mySocket.Send(bSendData,
                     bSendData.Length,0)) == -1)
                Console.WriteLine("Socket Error cannot Send Packet");
            else
            {
                Console.WriteLine("No. of bytes send {0}" , numBytes);
            }
        }
        else
            Console.WriteLine("Connection Dropped....");
    }
    catch (Exception  e)
    {
        Console.WriteLine("Error Occurred : {0} ", e );
    }
}

We now have all the building blocks ready, now we will delve into the key function of our application.


public void StartListen()
{

    int iStartPos = 0;
    String sRequest;
    String sDirName;
    String sRequestedFile;
    String sErrorMessage;
    String sLocalDir;
    String sMyWebServerRoot = "C:\\MyWebServerRoot\\";
    String sPhysicalFilePath = "";
    String sFormattedMessage = "";
    String sResponse = "";


    while(true)
    {
        //Accept a new connection
        Socket mySocket = myListener.AcceptSocket() ;

        Console.WriteLine ("Socket Type " +     mySocket.SocketType ); 
        if(mySocket.Connected)
        {
            Console.WriteLine("\nClient Connected!!\n==================\n
             CLient IP {0}\n", mySocket.RemoteEndPoint) ;


            //make a byte array and receive data from the client 
            Byte[] bReceive = new Byte[1024] ;
            int i = mySocket.Receive(bReceive,bReceive.Length,0) ;


            //Convert Byte to String
            string sBuffer = Encoding.ASCII.GetString(bReceive);


            //At present we will only deal with GET type
            if (sBuffer.Substring(0,3) != "GET" )
            {
                Console.WriteLine("Only Get Method is supported..");
                mySocket.Close();
                return;
            }


            // Look for HTTP request
            iStartPos = sBuffer.IndexOf("HTTP",1);


            // Get the HTTP text and version e.g. it will return "HTTP/1.1"
            string sHttpVersion = sBuffer.Substring(iStartPos,8);


            // Extract the Requested Type and Requested file/directory
            sRequest = sBuffer.Substring(0,iStartPos - 1);


            //Replace backslash with Forward Slash, if Any
            sRequest.Replace("\\","/");


            //If file name is not supplied add forward slash to indicate 
            //that it is a directory and then we will look for the 
            //default file name..
            if ((sRequest.IndexOf(".") <1) && (!sRequest.EndsWith("/")))
            {
                sRequest = sRequest + "/"; 
            }
            //Extract the requested file name
            iStartPos = sRequest.LastIndexOf("/") + 1;
            sRequestedFile = sRequest.Substring(iStartPos);


            //Extract The directory Name
               sDirName = sRequest.Substring(sRequest.IndexOf("/"), 
                       sRequest.LastIndexOf("/")-3);

The code is self-explanatory. It receives the request, converts it into string from bytes then look for the request type, extracts the HTTP Version, file and directory information.

Imtiaz Alam is a Senior Developer, currently residing in Phoenix, Arizona. He has more than five years of development experience in developing Mirosoft based Solution.

Imtiaz Alam is a Senior Developer, currently residing in Phoenix, Arizona. He has more than five years of development experience in developing Mirosoft based Solution. He can be reached at alamimtiaz@hotmail.com

Comments

  • Re: [1775] Create your own Web Server using C#

    Posted by michemal on 22 Jun 2007

    I was looking for a web server and came across a couple of other
    options, of course there is cassni / aka webdev but i tried this http://www.neokernel.com serv...

  • compression?

    Posted by rahulgarware on 01 Dec 2005

    How do you extend this to enable HTTP data compression?
    header:
    content-encoding=gzip

  • No Entry Point

    Posted by Aschehoug on 29 Jul 2005

    This code has no entry point. When compiling, I only get "error CS5001: Program 'c:\MyWebServer\MyWebServer.exe' does not have an entry point defined". I know that it is looking for a "Main()", but I ...

  • Posted by Michael H on 07 Sep 2004

    Hehe, I moved permanently to C# only a month or 2 after
    creating this thread :).

    I now have trouble reading VB code. Array access looks like
    function calls, etc... :cool:

  • Posted by SErhio on 07 Sep 2004

    i find c# more comprehensible then VB.
    for a C# programmer is more difficult for read VB code...