用 C 语言构建一个简单的 HTTP 服务器
我需要用 C 语言构建一个简单的 HTTP 服务器。有什么指导吗? 链接? 样品?
I need to build a simple HTTP server in C. Any guidance? Links? Samples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用特定于平台的套接字函数来封装 HTTP 协议,就像 Apache 背后的人所做的那样。
Use platform specific socket functions to encapsulate the HTTP protocol, just like guys behind Apache did.
http://www.manning.com/hethmon/ -- 《Paul 的 HTTP 图解指南》 Manning 的《S. Hethmon》是一本非常好的学习 HTTP 协议的书,对于实现/扩展它的人来说非常有用。
http://www.manning.com/hethmon/ -- "Illustrated Guide to HTTP by Paul S. Hethmon" from Manning is a very good book to learn HTTP protocol and will be very useful to someone implementing it /extending it.
有一个有更多回复的重复。
尚未提及的候选者之一是 spserver。
There is a duplicate with more responses.
One candidate not mentioned yet is spserver.
我建议查看诸如 lighthttpd 之类的源代码。
I'd suggest looking at the source to something like lighthttpd.
HTTP 规范 和 Firebug 在我必须做作业时对我非常有用。
祝你好运。 :)
The HTTP spec and Firebug were very useful for me when I had to do it for my homework.
Good luck with yours. :)
我自己写了一个,你可以使用。 这个工作有 sqlite,是线程安全的,并且是用 C++ for UNIX 编写的。
您应该能够将其拆开并使用 C 兼容代码。
http://code.google.com/p/mountain-cms/
I have written my own that you can use. This one works has sqlite, is thread safe and is in C++ for UNIX.
You should be able to pick it apart and use the C compatible code.
http://code.google.com/p/mountain-cms/
在端口 80 上打开 TCP 套接字,开始侦听新连接,实现此。
根据您的目的,您几乎可以忽略所有内容。 最简单的是,您可以为每个请求发送相同的响应,这仅涉及向套接字写入文本。
Open a TCP socket on port 80, start listening for new connections, implement this.
Depending on your purposes, you can ignore almost everything. At the easiest, you can send the same response for every request, which just involves writing text to the socket.
Mongoose(以前的简单 HTTP 守护进程)非常好。 特别是,它是可嵌入的,并且可以在 Windows、Windows CE 和 UNIX 下编译。
Mongoose (Formerly Simple HTTP Daemon) is pretty good. In particular, it's embeddable and compiles under Windows, Windows CE, and UNIX.
看看 nweb(Nigel 的 Web 服务器),“一个小型、安全的 Web 服务器 [...],只有 200 行 C 源代码”:
https://drive.google.com/file/d/0B3msld7qnNOhN1NXaFIwSFU2Mjg/view?usp=sharing&resourcekey=0-ngY 0neP78dxJKlFv0PJoDQ
http://www.ibm.com/developerworks/systems/library/es-nweb/本文包含伪代码、解释和注释。
编辑:IBM 的链接已失效。 我已将网页的 PDF 格式保存到 Google 云端硬盘。 以下是代码下载:
https:// drive.google.com/file/d/0B3msld7qnNOhSGZGdDJJMmY0VHM/view?usp=sharing&resourcekey=0-xkbf4mv0gN1sZrhBjt86UQ
@ ankushagarwal 做了一些更改并在 GitHub 上上传了他的版本:
https://github.com/ankushagarwal/nweb
Look at nweb (Nigel's Web Server), "a tiny, safe web server [...] with only 200 lines of C source code":
https://drive.google.com/file/d/0B3msld7qnNOhN1NXaFIwSFU2Mjg/view?usp=sharing&resourcekey=0-ngY0neP78dxJKlFv0PJoDQ
http://www.ibm.com/developerworks/systems/library/es-nweb/The article includes pseudocode, explanations, and comments.
EDIT: IBM's link has died. I have saved a PDF of the webpage to Google Drive. Here is the code download:
https://drive.google.com/file/d/0B3msld7qnNOhSGZGdDJJMmY0VHM/view?usp=sharing&resourcekey=0-xkbf4mv0gN1sZrhBjt86UQ
@ankushagarwal has made a few changes and uploaded his version on GitHub:
https://github.com/ankushagarwal/nweb
HTTP 服务器在概念上很简单:
它会变得更加困难,具体取决于您想要支持多少 HTTP - POST 稍微复杂一点,脚本,处理多个请求等。
但基础非常简单。
An HTTP server is conceptually simple:
It gets more difficult depending on how much of HTTP you want to support - POST is a little more complicated, scripts, handling multiple requests, etc.
But the base is very simple.
我建议你看看tiny httpd。 如果您想从头开始编写它,那么您需要彻底阅读RFC 2616。 使用 BSD 套接字 在非常低的级别访问网络。
I suggest you take a look at tiny httpd. If you want to write it from scratch, then you'll want to thoroughly read RFC 2616. Use BSD sockets to access the network at a really low level.
我建议您看一下:编写客户端和服务器的实用指南
必须以增量步骤实现的是:
I'd recommend that you take a look at: A Practical Guide to Writing Clients and Servers
What you have to implement in incremental steps is: