Good programmers share keep-alive difference between keep-alive and http-tcp

keep-alive difference between keep-alive and http-tcp

1、HTTP Keep-Alive

  In the early http, each open http request requires a tpc socket connector, after a single use and disconnects the tcp connection.

  Use can improve the keep-alive state, which can continue to send data in multiple without a TCP connection is disconnected. By using a keep-alive mechanism, it reduces the number of tcp connection is established, the state also means less Time_Wait connection, thus improving performance and increase throughput httpd server.

  However, keep-alive it is not a free lunch, tcp connection for a long time can easily lead to invalid occupy system resources. Misallocation of keep-alive, will be greater than the losses caused by connection reuse. Therefore, the correct set keep-alive timeout time is very important.

2、tcp keepalive

  After the link is established, and if the application or upper-layer protocol has not sending data, or long intervals only made once the data, how to determine when the other side also online link for a long time there is no data packet transmission, in the end is really no dropped calls or data transmission link need not need to keep this case in the TCP protocol design is the need to take into account.

  TCP protocol to solve this problem through a particularly clever way, when over a period of time, TCP automatically sends a data packet to empty the other side, if the other party to respond to this message, indicating that the other side also online link can remain If the other party does not return packets, and retry several times after the link is considered lost, there is no need to maintain the link.

3、keepalvie timeout

  Httpd daemon, generally provide a keep-alive timeout time setting parameters. For example, the nginx keepalive_timeout, and Apache KeepAliveTimeout. This keepalive_timout time value means: tcp connection http generated after a transfer after the last one response, but also hold live keepalive_timeout seconds later, I began to close the connection.

  After the httpd daemon after sending a response, should immediately take the initiative to close the corresponding tcp connection, after setting keepalive_timeout, httpd daemon will say: "Wait a minute browser, and see there is no request to come", the other is keepalive_timeout time. If the daemon in the waiting time, never received browser sent me http request, then close the http connection.

4, tcp Keepalive timer preservation

  HTTP is the Keepalive, as the name implies, the object is to extend the connection time, in order to transmit a plurality of HTTP requests in the same connection. HTTP server typically provide Keepalive Timeout parameters used to determine the connection remains long, and when the connection is closed. When the connection Keepalive function, for a request sent by the client, the server sends a response, and then starts counting, if the elapsed time after Timeout, the client no longer sends a request over the connection server put off, no longer remain connected.

  The TCP Keepalive, is trickery, aimed to see each other there is no exception occurs, if there is an exception to timely close the connection. When the transmission of the parties do not take the initiative to close the connection, even if the two sides did not exchange any data connection also it has been effective. If this time caused connection is not available, how does this end that the information on the abnormal end, the intermediate network? The answer is keep-alive timer. It is time out from time to time, it will check whether the connection is idle for too long after a timeout, if idle for more than the set time, it will send a probe packet. Then responds by side, whether the expected response to determine whether the peer normal, if not normal, the connection is closed to take the initiative, without waiting for the off HTTP layer. When the server sends probe packets, the client may be in four different situations: still operating normally, has collapsed, has crashed and restarted, because the intermediate link problems unreachable. Under different circumstances, the server will not get the same feedback.

5、http keep-alive与tcp keep-alive

  http keep-alive and tcp keep-alive, not the same thing, the intent is the same. http keep-alive is to allow tcp live a little longer, in order to transmit multiple http on the same connection, improve the efficiency of the socket. And preservation is tcp keep-alive TCP connection status detecting mechanism of the TCP.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10944804.html
Recommended