HTTP持久连接 [英] HTTP Persistent connection

查看:518
本文介绍了HTTP持久连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Linux套接字接口在C中实现一个简单的HTTP服务器我遇到了一些我希望它拥有的特性的困难,即持久连接。使用单独的TCP连接一次发送一个文件相对容易,但它似乎不是非常有效的解决方案(例如考虑多个握手)。无论如何,服务器应该在一个TCP连接期间处理多个请求(HTML,CSS,图像)。你能给我一些解决问题的线索吗?

Trying to implement a simple HTTP server in C using Linux socket interface I have encountered some difficulties with a certain feature I'd like it to have, namely persistent connections. It is relatively easy to send one file at a time with separate TCP connections, but it doesn't seem to be very efficient solution (considering multiple handshakes for instance). Anyway, the server should handle several requests (HTML, CSS, images) during one TCP connection. Could you give me some clues how to approach the problem?

推荐答案

这很简单 - 只是不要关闭TCP连接写完回复后。

It is pretty easy - just don't close the TCP connection after you write the reply.

有两种方法可以做到这一点,流水线和非流水线。

There are two ways to do this, pipelined, and non pipelined.

在非流水线实现中,您在套接字上读取一个http请求,处理它,将其从套接字中写回,然后尝试读取另一个。继续这样做,直到远程方关闭套接字,或者在大约10秒后停止在套接字上获得请求后自行关闭套接字。

In a non-pipelined implementation you read one http request on the socket, process it, write it back out of the socket, and then try to read another one. Keep doing that until the remote party closes the socket, or close it yourself after you stop getting requests on the socket after about 10 seconds.

在流水线实现中,读为套接字上的许多请求,并行处理它们,然后将它们全部写回到套接字上,顺序与接收它们的顺序相同。你有一个线程一直在读取请求,另一个线程再次写出来。

In a pipelined implementation, read as many requests as are on the socket, process them all in parallel, and then write them all back out on the socket, in the same order as your received them. You have one thread reading requests in all the time, and another one writing them out again.

你不必这样做,但你可以做广告你支持持久连接和流水线操作,在回复中添加以下标题:

You don't have to do it, but you can advertize that you support persistent connections and pipelining, by adding the following header in your replies:

Connection: Keep-Alive

阅读:
http://en.wikipedia.org/wiki/HTTP_persistent_connection

顺便说一句,在实践中,持久性没有巨大优势连接。与将数据读写到网络套接字所花费的时间相比,管理握手的开销非常小。关于持久连接的性能优势存在争议。一方面,在负载很重的情况下,保持连接打开意味着在TIME_WAIT中系统上的套接字要少得多。另一方面,因为你让套接字保持打开状态10秒,你在任何给定的时间都会比在非持久模式下打开更多的套接字。

By the way, in practice there aren't huge advantages to persistent connections. The overhead of managing the handshake is very small compared to the time taken to read and write data to network sockets. There is some debate about the performance advantages of persistent connections. On the one hand under heavy load, keeping connections open means many fewer sockets on your system in TIME_WAIT. On the other hand, because you keep the socket open for 10 seconds, you'll have many more sockets open at any given time than you would in non-persistent mode.

如果您对提高自编写服务器的性能感兴趣 - 您可以做的最好的事情就是实现基于事件的套接字管理系统,以提高服务器网络前端的性能。查看libev和eventlib。

If you're interested in improving performance of a self written server - the best thing you can do to improve performance of the network "front-end" of your server is to implement an event based socket management system. Look into libev and eventlib.

这篇关于HTTP持久连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆