如何在Linux上用C/C++编写Web服务器

Jac*_*ack 34 c c++ linux

我正在研究在Linux平台上开发一个小的(读取:基本的)Web服务器,我不知道从哪里开始.

我希望它能做的是:

  • 听一个特定的端口
  • 获取HTTP帖子并获取请求
  • 适当回应
  • 无需会话管理
  • 必须是C或C++
  • 必须在启动时作为服务运行

我熟悉HTTP标头,是一位经验丰富的PHP和.Net Web开发人员,但我不知道从哪里开始这项任务.

你能用一些资源建议我弥合学习曲线吗?

mct*_*ylr 39

从上到下,您需要了解:

  • HTTP协议
  • TCP服务器 - BSD套接字编程
  • 编写基本的Unix守护进程(持久性服务)
  • 流程管理(fork)
  • 解析文本(读取配置文本文件)
  • 文件处理(I/O)
  • 调试C/C++编程:)

因此,您必须学习编写基本的Unix应用程序,用于TCP/IP网络编程的BSD套接字编程以及HTTP协议.

常用文本包括:

Unix应用程序开发:

  • Unix环境中的高级编程,Stevens和Rago
  • 高级Unix编程

TCP/IP(套接字)编程:

  • Unix网络编程,第1卷Stevens等
  • TCP/IP Illustrated,史蒂文斯
  • 使用TCP/IP进行Inenetnetworking,第3卷,Comer

HTTP协议

  • RFC包括
  • HTTP v1.1的RFC 2616,
  • 用于pre-v1.1的RFC 2068
  • 加上其他依赖于支持(压缩,URI/URL)和完整性


Ked*_*are 18

对于SIMPLE/BASIC/ULTRA-LIGHT HTTP服务器,程序流应该类似(伪代码):

----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
Run Code Online (Sandbox Code Playgroud)

所以你应该学习线程,进程间通信(如果你想与解释器交互),Socket编程和HTTP协议.


N 1*_*1.1 8

所有细节都不能在这里解释
访问http://www.linuxhowtos.org/C_C++/socket.htm以使用C创建基本服务器.
另一个由IBM提供:http://www.ibm.com/developerworks/systems/library /es-nweb/index.html


Dir*_*tel 5

您始终可以从现有代码库开始。boa可能是一个开始,因为它很小,用 C 实现,适合您的“启动时启动”要求;详细信息例如在 Debian / Ubuntu 软件包中。