How do the options of my socket (socket, which I've got after accept() ) related to HTTP header "Connection: Keep-Alive"? I know, that if I want to keep my socket alive, I need to set the following options: SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL, and TCP_KEEPCNT. For instance, if I want to wait for 10 seconds and then send 10 probes maximum with an interval of 2 seconds, before dropping the connection, I will write something like this:
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(int);
int idle = 10;
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(int));
int interval = 2;
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(int));
int maxpkt = 10;
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &maxpkt, sizeof(int));
And if I intend to keep the connection open for at most five more transactions, or until it has sat idle for two minutes, I will send the next headers within my response:
Connection: Keep-Alive
Keep-Alive: max=5, timeout=120
It's all OK, but I don't understand how these things relate to each other. How should I add supporting of Keep-Alive attribute to my HTTP-server?
I will be grateful for any help. Thank you in advance.