Create your own Web Server using C#

The Code (3)

// Identify the Physical Directory
if ( sDirName == "/")
    sLocalDir = sMyWebServerRoot;
    //Get the Virtual Directory
    sLocalDir = GetLocalPath(sMyWebServerRoot, sDirName);

Console.WriteLine("Directory Requested : " +  sLocalDir);

//If the physical directory does not exists then
// dispaly the error message
if (sLocalDir.Length == 0 )
    sErrorMessage = "<H2>Error!! Requested Directory does not exists</H2><Br>";
    //sErrorMessage = sErrorMessage + "Please check data\\Vdirs.Dat";

    //Format The Message
    SendHeader(sHttpVersion,  "", sErrorMessage.Length, " 404 Not Found", ref mySocket);

    //Send to the browser
    SendToBrowser(sErrorMessage, ref mySocket);



Note: Microsoft Internet Explorer usually displays a 'friendy' HTTP Error Page if you want to display our error message then you need to disable the 'Show friendly HTTP error messages' option under the 'Advanced' tab in Tools->Internet Options. Next we look if the directory name is supplied, we call GetLocalPath function to get the physical directory information, if the directory not found (or does not mapped with entry in Vdir.Dat) error message is sent to the browser.. Next we will identify the file name, if the filename is not supplied by the user we will call the GetTheDefaultFileName function to retrieve the filename, if error occurred it is thrown to browser.

// Identify the File Name

//If The file name is not supplied then look in the default file list
if (sRequestedFile.Length == 0 )
    // Get the default filename
    sRequestedFile = GetTheDefaultFileName(sLocalDir);

    if (sRequestedFile == "")
        sErrorMessage = "<H2>Error!! No Default File Name Specified</H2>";
        SendHeader(sHttpVersion,  "", sErrorMessage.Length, " 404 Not Found", 
               ref mySocket);
        SendToBrowser ( sErrorMessage, ref mySocket);




Then we need to identify the Mime type:

// Get TheMime Type

String sMimeType = GetMimeType(sRequestedFile);

//Build the physical path
sPhysicalFilePath = sLocalDir + sRequestedFile;
Console.WriteLine("File Requested : " +  sPhysicalFilePath);

Now the final steps of opening the requested file and sending it to the browser..

if (File.Exists(sPhysicalFilePath) == false)

    sErrorMessage = "<H2>404 Error! File Does Not Exists...</H2>";
    SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket);
    SendToBrowser( sErrorMessage, ref mySocket);


    int iTotBytes=0;

    sResponse ="";

    FileStream fs = new FileStream(sPhysicalFilePath, FileMode.Open,     FileAccess.Read,
    // Create a reader that can read bytes from the FileStream.

    BinaryReader reader = new BinaryReader(fs);
    byte[] bytes = new byte[fs.Length];
    int read;
    while((read = reader.Read(bytes, 0, bytes.Length)) != 0) 
        // Read from the file and write the data to the network
        sResponse = sResponse + Encoding.ASCII.GetString(bytes,0,read);

        iTotBytes = iTotBytes + read;


    SendHeader(sHttpVersion,  sMimeType, iTotBytes, " 200 OK", ref mySocket);
    SendToBrowser(bytes, ref mySocket);
    //mySocket.Send(bytes, bytes.Length,0);



You might also like...


About the author

Imtiaz Alam United States

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.

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski