How a Web server actually works ~ with C source code

Posted on in Categories Beyond nixCraft, C Programming, Howto, News, UNIX last updated January 9, 2008

Do you wonder how to write a program that accepts incoming messages with a network socket? Have you ever just wanted your own Web server to experiment and learn with?

Have you ever wondered how a Web server actually works? Experiment with nweb — a simple Web server with only 200 lines of C source code. In this article, Nigel Griffiths provides a copy of this Web server and includes the source code as well. You can see exactly what it can and can’t do.

Well, look no further — nweb is what you need. This is a simple Web server that has only 200 lines of C source code. It runs as a regular user and can’t run any server-side scripts or programs, so it can’t open up any special privileges or security holes.

This article covers:

  • What the nweb server program offers
  • Summary of C functions features in the program
  • Pseudo code to aid understanding of the flow of the code
  • Network socket system calls used and other system calls
  • How the client side operates
  • C source code

nweb only transmits the following types of files to the browser :

  • Static Web pages with extensions .html or .htm
  • Graphical images such as .gif, .png, .jgp, or .jpeg
  • Compressed binary files and archives such as .zip, .gz, and .tar
  • If your favorite static file type is not in this list, you can simply add it in the source code and recompile to allow it.

Read more at IBM developerworks

28 comment

  1. how to Write a simple web server in C. This web server only has to respond to GET requests. A web server that serves the same content irrespective of the URL in the GET request is worth 80% of the marks. For further 20% of the marks your server must serve contents based on the file names in the URLs in GET requests.please help me with your source code.

  2. I want to know how to build program for web-server in c
    so that client send req for file to server and server gie rpl accordingly

  3. Hi,
    i am getting problems while programming http server. Can you send me your source code. It will be really a great help.
    Thans alot.

  4. pls help me in developing a web server and tracking the links tracked by an online user . i mean generation of on fly log file

Leave a Comment