开发者

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

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

Improve this question

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开发者_如何学JAVA 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?


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


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.


All details cant be explained here
Visit http://www.linuxhowtos.org/C_C++/socket.htm for creating a basic server using C.
Another one by IBM : http://www.ibm.com/developerworks/systems/library/es-nweb/index.html


You could always start with an existing code base. boa may be a start as it is small, implemented in C and suitable for your 'start on boot' requirement; details are e.g. in the Debian / Ubuntu package.


http://en.wikipedia.org/wiki/Comparison_of_lightweight_web_servers

thank you AGAIN wikipedia

BTW - you might want to Google "embedded web server open source"

(www).ibm.com/developerworks/web/library/wa-ltwebserv/


With libevent library, you can write a web server in 40 lines of c code.

http://www.ruilog.com/article/view/5249.html

If you want create it from ground up, then you can reference open source webserver written in c like lighttpd, apache, nginx.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜