How a Web server actually works ~ with C source code

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

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
28 comments… add one
  • sheetal May 11, 2012 @ 17:27

    hey can any1 plz tel how to run tis web server

  • JoJeJa Apr 17, 2012 @ 11:47

    Can you please send me the source code in C for HTTP web server?

  • Tejasvi Mar 2, 2012 @ 7:21

    Thanks for ur reply..
    dis is my email id:
    and thanks alot..

  • Tejasvi Feb 26, 2012 @ 18:27

    i’m doing my final year project on the topic web server.. can u plz help me by providing source code??


    • abid khan afridi Feb 29, 2012 @ 15:16

      yes tejasvi i can give u source code what is ur email adress

  • Nandhini Jan 18, 2012 @ 4:35

    i want c codings for web server load balancing…can u send me such coding to my mail?

  • Eldor Sep 9, 2011 @ 9:03

    How can we get the source code?
    Thank you

  • harita Aug 17, 2011 @ 5:25

    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

  • iC0d3R Jul 4, 2011 @ 21:20

    thanks for helping,really thanks
    can you send for me the C source code?
    i need it…thanks a lot…

    • abid khan afridi Jul 8, 2011 @ 18:03

      you have a source code ????

  • zazinyo Apr 22, 2011 @ 22:43

    hı cloud you send your source code please thanks for you attention

  • Raj Dec 23, 2010 @ 6:29

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

  • icem Dec 11, 2010 @ 1:42

    Hiya im trying to program an embedded web sever, but im finding the coding hard, can u please send me the source code

  • Kover Sep 1, 2010 @ 1:04

    i am currently struggling with programming a webserver, would you please to send me the source code, thanks a lot

  • abc Aug 27, 2010 @ 15:35

    heya! i have just started with network programming. if u can plzz help me by sending ur source code.

  • arshad naveed Aug 20, 2010 @ 6:26

    I am new to network programming. Please send me the source code of your program.

  • Oyundari May 17, 2010 @ 9:30

    plz help me i’m student. i need simple web server using socket source code. how to write mod_php5 source code. my e-mail

  • Mahendran May 2, 2010 @ 6:34


    I am planning to do a project in web server. I want a reference source code..Can you send to my mail id :


  • ramesh Sep 30, 2009 @ 10:51

    could plz help me to geta web server code in cpp

  • Santosh Jul 29, 2009 @ 4:43

    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

  • sanjee Jun 17, 2009 @ 3:23

    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.

  • Ron May 4, 2009 @ 3:00

    want advanced code in C web server
    thanks a lot..

  • HARISH Apr 21, 2009 @ 15:57

    iam working a project which will send and receive data from webserver. Your webserver application is suitable for me to develop tmy projet very fast. Can you please forward the source code of the webserver using HTTP to my mail id as follows:

  • tao chen Mar 9, 2009 @ 17:40

    I am a student learning how to write a web server, please help me with your source code.

    • sonam lhamo Dec 18, 2013 @ 9:59

      i student and i m doing web designing…
      plez text me simple code

  • Mahi Oct 23, 2008 @ 11:34

    i need the C code for HTTP server..Please help me.

  • nils Sep 16, 2008 @ 17:16

    thanks for the tip!
    it helped much

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @