Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Persistent connection

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?

like image 635
Clueless Avatar asked Dec 26 '22 02:12

Clueless


1 Answers

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.

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

Read this: http://en.wikipedia.org/wiki/HTTP_persistent_connection

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.

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.

like image 128
Rafael Baptista Avatar answered Dec 28 '22 23:12

Rafael Baptista