Library tutorials & articles

Create your own Web Server using C#

Summary

This article explains how to write a simple web server application using C#. Though it can be developed in any .net supported language, I chose C# for this example. The code is compiled using beta2. Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]. It can be used with Beta1 with some minor modification. This application can co-exists with IIS or any web server, the key is to choose any free port. I assume that the user has some basic understanding of .net and C# or VB.Net. This Web server just returns html formatted files and also supports images. It does not supports any kind of scripting. I have developed a console-based application for simplicity.

Comments

  1. 22 Jun 2007 at 20:33
    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 server because it seemed to have good samples for how to configure it for ssl, passwords, etc.

  2. 01 Dec 2005 at 09:00
    How do you extend this to enable HTTP data compression?
    header:
    content-encoding=gzip
  3. 29 Jul 2005 at 18:45
    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 am pretty much a newbie, and I don't know where to add that.
  4. 07 Sep 2004 at 13:08

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

  5. 07 Sep 2004 at 12:50

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

  6. 07 Sep 2004 at 12:48

    hello,
    i need to write a service Windows of the base a the same server.
    I am traiyng to use this code in the WinService(c# .NET), but a error was occured:
    the system has stopped listen etc...


    I think the problem is in the
    while(true){...}
    and i think this need make sleep the thread for sometime....

  7. 02 Jul 2002 at 00:21

    How about the POST type,


    I noticed that when client send a POST type,
    only the http headers appear and nothing follows, even there are attachments.

  8. 06 Jun 2002 at 15:33
    Even though I am only a VB.NET programer,
    I still understood all the code.
    gotta love .NET!
  9. 25 Oct 2001 at 21:25

    It's amazing how little code it takes to write a functioning web server these days.  Consider the old MFC samples for comparison.


    You might consolidate your text file parsing code.  The whole semicolon searching system is pretty generic.  Or go with XML like you said.


    You should support the HEAD verb b/c dropping the connection when it's not a GET request will anger many browsers in terms of caching.


    You shouldn't read the whole file into memory using a string concatenation loop.  The socket class probably has a method to efficiently transmit an entire file.  If it doesn't, you should have your loop read a little and write a little, otherwise those multi-megabyte flash and BMP and MP3 files will start to kill your server.

  10. 01 Jan 1999 at 00:00

    This thread is for discussions of Create your own Web Server using C#.

Leave a comment

Sign in or Join us (it's free).

Imtiaz Alam 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.

We'd love to hear what you think! Submit ideas or give us feedback