Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a web server in C/C++ on linux [closed]

Tags:

c++

c

linux

I am looking into developing a small (read:rudimentary) web server on a linux platform and I have no idea where to start.

What I want it to be able to do is:

  • Listen on a specific port
  • Take HTTP post and get requests
  • Respond appropriately
  • No session management required
  • Has to be in C or C++
  • Has to run as a service on boot

I am familiar with HTTP headers and am an experienced PHP and .Net web developer, but I am not sure where to start with this task.

Can you advise me with some resources to bridge the learning curve?

like image 221
Jack Avatar asked Feb 26 '10 00:02

Jack


People also ask

What is webserver in C?

A web server is a piece of software that accepts HTTP requests (e.g. GET requests for HTML pages), and returns responses (e.g. HTML pages).


2 Answers

From top-down, you'll need to know about:

  • HTTP Protocol
  • TCP server - BSD socket programming
  • writing a basic Unix daemon (persistent service)
  • process management (fork)
  • parsing text (read a configuration text file)
  • file handling (I/O)
  • debugging C / C++ programming :)

So you will have to learn about writing a basic Unix application, BSD socket programming for the TCP/IP network programming, and the HTTP protocol.

Commonly used texts include:

Unix application development:

  • Advanced Programming in the Unix Environment, Stevens & Rago
  • Advanced Unix Programming

TCP/IP (sockets) programming:

  • Unix Network Programming, Volume 1 Stevens et all
  • TCP/IP Illustrated, Stevens
  • Ineternetworking with TCP/IP, Volume 3, Comer

HTTP Protocol

  • RFCs including
  • RFC 2616 for HTTP v1.1,
  • RFC 2068 for pre-v1.1
  • plus others depending on support (compression, URI / URL) and completeness
like image 154
mctylr Avatar answered Sep 27 '22 20:09

mctylr


For a SIMPLE/BASIC/ULTRA-LIGHT HTTP Server, the program flow should be something like that (in pseudo-code):

----Main thread---- Load settings while true do     Wait for connection     Connection received, create a new thread and transfer this connection to this thread. end  ----Connection thread---- Analyze request if dynamic content do     Dump the HTTP request and send it to the interpreter     Wait for response from the interpreter     Read response header from the interpreter     Stream response else if static content do     Load requested file     Stream file content end (Optional) Cache the response if size < X Send the response Close the socket 

So you should learn Threading, Interprocess-communication (if you want to interact with an interpreter), Socket programming and the HTTP Protocol.

like image 40
Kedare Avatar answered Sep 27 '22 22:09

Kedare