Connection: Keep-Alive

In the laboratory the morning reading, Young let me help solve problems on the project, the problem is not difficult, so I taught him capture resolved, let him down fiddler computer but it does not show up request, I tried it too, filter also I did not open, and suddenly realized that the problem might be 360, because fiddler way through http proxy, 360 are likely to prevent third-party software on the browser agent, then turn off the 360, restart the browser was finally able to capture the happy .

I told him the debugging process F12 hook disable cache, then the eye is attracted by one word --- Connection: Keep-Alive

I was very puzzled, http obviously no connection stateless, so what keepalive here refers to?

In fact, it is a TCP multiplexing, every http request requires three-way handshake tcp, waste of resources and time, so we can keep tcp channel connection for some time, such a tcp connection can be maintained several times http request.


 

On this basis, we also need to clear up some other term: long polling, the polling period

Long connection to tcp multiplexing, then these two words is to do what? Start with an example:

Short polling believe we are not difficult to understand, like you do now electricity supplier in a listings page, this detail screen has a field that inventory (to believe that everyone is familiar with, or not open Taobao Jingdong can be found this page). And this requires real-time inventory changes, server maintenance and inventory in real consistent, how to achieve?

Long polling this time there was, in fact, the biggest difference between long and short polling polling is polling to serve the short end of the query time, no matter the amount of inventory has not changed, the server immediately return the results. And polling is not long, the long polling, the server if it is detected no change in the amount of stock, it will suspend the current request period of time (this time is also called the timeout period, typically tens of seconds). At this time, the server to detect inventory levels have not changed, change is detected to return immediately, otherwise wait until the timeout.

Obviously, the number of clients using long polling request will be a significant reduction (which means savings in network traffic, after all, every request sent will occupy download client traffic upload traffic and server). However, if a large number of clients are requesting inventory, service end use multiple threads to suspend the request, the pressure so that the server is too big now. So long polling is also applicable scene.


Polling and differentiate under long length of connecting their most fundamental difference is:

The first difference is the way the decision, whether a TCP connection is a persistent connection, by setting the HTTP Connection Header determined, but both sides need to set effective. A wrap mode and whether the long polling, the server is based on the approach to the decision, there is no relationship with the client.

The second difference is the way to achieve, to a predetermined length is connected and implemented by agreement. The length of the polling is done manually suspend request server programmatically.


 

In short, remember this: long connection is a TCP connection, rather than HTTP connections. HTTP protocol is based on a request / response model, so long as the response to the server, this is over an HTTP connection, the TCP connection is a bidirectional channel, it is not maintained for a period of time off, and therefore have a real TCP connection long and short connection that said connection

Guess you like

Origin www.cnblogs.com/ZoHy/p/11331058.html