现在的位置: 首页 > 综合 > 正文

基于boost::asio的http服务器

2013年09月03日 ⁄ 综合 ⁄ 共 981字 ⁄ 字号 评论关闭

基于boost::asio的http服务器
————
博客,http://blog.csdn.net/shunqiziranhao007/article/details/8737629
日期,2013年3月29日
————
这是boost asio库的一个例子(我修改了部分代码后实现后面的效果),实现了http1.0的http请求报文和http应答报文。蛮有意思的。

————
http
————
具体可以参看,《计算机网络-自顶向下V4cn》p61。
更多可以看RFC 2616。

http报文分为两种,请求报文和应答报文。

————
http请求报文
————
分为3部分,请求行1行,首部行若干,实体主体。

请求行的格式是,方法,空格,URI,空格,协议的版本,如,
GET /index.html HTTP/1.1 crlf
然后是首部行,如
Host: www.xxx.com crlf
Connection: close crlf
crlf
最后是实体主体

————
http应答报文
————
3部分,状态行1行,首部行若干行,实体主体。如,

HTTP/1.1 200 OK crlf
Connection: close crlf
Date: ...crlf
Server: ...crlf
Last-Modified: ...crlf
Content-Length: ...crlf
Content-Type: ...crlf
实体主体

————
下面是程序效果。

服务器的根目录,
alan@alan-pc:http$ pwd
/home/alan/test/http
alan@alan-pc:http$ cat index.html
<html>
<hr>
<b>基于boost::asio的http服务器,你好!</b>
<hr>
</html>

开启http服务器,
alan@alan-pc:server$ ./http_server.exe 0.0.0.0 10080 /home/alan/test/http

客户端访问http服务器,
alan@alan-pc:client$ ./async_client.exe 0.0.0.0 10080 /index.html
Content-Length xh: 78
Content-Type xh: text/html

<html>
<hr>
<b>基于boost::asio的http服务器,你好!</b>
<hr>
</html>

浏览器访问http服务器,

抱歉!评论已关闭.