如何在 Linux 上用 C/C++ 编写 Web 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2338775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write a web server in C/C++ on linux
提问by Hyman
I am looking into developing a small (read:rudimentary) web server on a linux platform and I have no idea where to start.
我正在考虑在 linux 平台上开发一个小型(阅读:基本)Web 服务器,但我不知道从哪里开始。
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
- 侦听特定端口
- 使用 HTTP post 并获取请求
- 适当回应
- 无需会话管理
- 必须是 C 或 C++
- 必须在启动时作为服务运行
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.
我熟悉 HTTP 标头,并且是一位经验丰富的 PHP 和 .Net Web 开发人员,但我不确定从哪里开始这项任务。
Can you advise me with some resources to bridge the learning curve?
你能给我提供一些资源来弥补学习曲线吗?
回答by mctylr
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 :)
- HTTP 协议
- TCP 服务器 - BSD 套接字编程
- 编写一个基本的 Unix 守护进程(持久服务)
- 进程管理(fork)
- 解析文本(读取配置文本文件)
- 文件处理 (I/O)
- 调试 C/C++ 编程 :)
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.
因此,您必须学习编写基本的 Unix 应用程序、用于 TCP/IP 网络编程的 BSD 套接字编程以及 HTTP 协议。
Commonly used texts include:
常用的文本包括:
Unix application development:
Unix应用开发:
- Advanced Programming in the Unix Environment, Stevens & Rago
- Advanced Unix Programming
- Unix 环境中的高级编程,Stevens & Rago
- 高级 Unix 编程
TCP/IP (sockets) programming:
TCP/IP(套接字)编程:
- Unix Network Programming, Volume 1 Stevens et all
- TCP/IP Illustrated, Stevens
- Ineternetworking with TCP/IP, Volume 3, Comer
- Unix 网络编程,第 1 卷 Stevens 等
- TCP/IP 图解,史蒂文斯
- 网络与 TCP/IP,第 3 卷,Comer
HTTP Protocol
HTTP 协议
回答by Kedare
For a SIMPLE/BASIC/ULTRA-LIGHT HTTP Server, the program flow should be something like that (in pseudo-code):
对于 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
So you should learn Threading, Interprocess-communication (if you want to interact with an interpreter), Socket programming and the HTTP Protocol.
所以你应该学习线程、进程间通信(如果你想与解释器交互)、套接字编程和 HTTP 协议。
回答by N 1.1
All details cant be explained here
Visit http://www.linuxhowtos.org/C_C++/socket.htmfor creating a basic server using C.
Another one by IBM : http://www.ibm.com/developerworks/systems/library/es-nweb/index.html
此处无法解释所有详细信息
访问http://www.linuxhowtos.org/C_C++/socket.htm以使用 C 创建基本服务器
。IBM 的另一个:http: //www.ibm.com/developerworks/systems/library /es-nweb/index.html
回答by Dirk Eddelbuettel
回答by Ryan
http://en.wikipedia.org/wiki/Comparison_of_lightweight_web_servers
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/
(www).ibm.com/developerworks/web/library/wa-ltwebserv/
回答by Sam Liao
With libevent library, you can write a web server in 40 lines of c code.
使用 libevent 库,您可以用 40 行 C 代码编写一个 Web 服务器。
http://www.ruilog.com/article/view/5249.html
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.
如果你想从头开始创建它,那么你可以参考用 c 编写的开源网络服务器,如 lighttpd、apache、nginx。