In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the long connection and short connection of HTTP". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand the long connection and short connection of HTTP".
HTTP long connection and short connection
The relationship between 1.HTTP Protocol and TCP/IP Protocol
The long connection and short connection of HTTP are essentially TCP long connection and short connection. HTTP belongs to the application layer protocol, which uses TCP protocol at the transport layer and IP protocol at the network layer. IP protocol mainly solves the problem of network routing and addressing, while TCP protocol mainly solves how to reliably deliver data packets above the IP layer, so that all packets sent by the originator are received at the other end of the network, and the order is consistent with the sending order. TCP is reliable and connection-oriented.
two。 How to understand that HTTP protocol is stateless
The HTTP protocol is stateless, which means that the protocol has no memory ability for transactions, and the server does not know what the client state is. That is, there is no connection between opening a web page on a server and opening a web page on that server before. HTTP is a stateless connection-oriented protocol, stateless does not mean that HTTP can not maintain TCP connections, let alone that HTTP uses the UDP protocol (connectionless).
3. What are long connections and short connections?
In HTTP/1.0, short connections are used by default. In other words, every time the browser and the server perform a HTTP operation, a connection is established, but the connection is interrupted at the end of the task. If a HTML or other type of Web page accessed by the client browser contains other Web resources, such as JavaScript files, image files, CSS files, etc., every time the browser encounters such a Web resource, it will establish a HTTP session.
However, starting from HTTP/1.1, long connections are used by default to maintain the connection characteristics. Using the long-connected HTTP protocol, this line of code will be added to the response header:
Connection:keep-alive
In the case of a persistent connection, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed, and if the client visits the web page on the server again, it will continue to use this established connection. Keep-Alive does not stay connected permanently, it has a hold time, which can be set in different server software (such as Apache). To realize the persistent connection, both the client and the server support the persistent connection.
The long connection and short connection of HTTP protocol are essentially the long connection and short connection of TCP protocol.
"
HTTP belongs to the application layer protocol and uses TCP protocol at the transport layer.
3.1 TCP connection
When the TCP protocol is used in network communication, a connection must be established between server and client before the real read-write operation. When the read-write operation is completed and the connection is no longer needed, they can release the connection. The establishment of the connection requires three handshakes, while the release requires four handshakes, so the establishment of each connection requires resource consumption and time consumption.
3.2 TCP short connection
Let's simulate the case of a TCP short connection. Client initiates a connection request to server, server receives the request, and then the two parties establish a connection. Client sends a message to server, and server responds to client, and then completes reading and writing. At this time, either of the two parties can initiate the close operation, but usually client initiates the close operation first. Why, the general server will not reply to the client immediately after closing the connection, of course, do not rule out special circumstances. Judging from the above description, short connections generally pass read and write operations only once between client/server
The advantage of short connection is that it is easy to manage, the existing connections are all useful connections, and no additional control means are needed.
3.3 TCP persistent connection
Next, let's simulate the situation of a persistent connection. Client initiates a connection to server, server accepts client connection, and the two sides establish a connection. After Client and server complete a read and write, the connection between them will not be actively closed, and subsequent read and write operations will continue to use this connection.
First of all, let's talk about the TCP keeping alive function mentioned in the TCP/IP detailed explanation. The keeping alive function is mainly provided for the server application. The server application wants to know whether the client host crashes, so that it can use resources on behalf of the customer. If the customer has disappeared, leaving a semi-open connection on the server, and the server is waiting for data from the client, the server should be waiting for the data from the client. The alive function is trying to detect such a semi-open connection on the server side.
If there is no action for a given connection within two hours, the server sends a probe message to the customer, and the client host must be in one of the following four states:
The customer host is still operating normally and is reachable from the server.
The customer's TCP response is normal, and the server knows that the other party is normal, and the server resets the live timer after two hours.
The customer host has crashed and is shutting down or rebooting. In either case, the customer's TCP does not respond. The server will not receive a response to the probe and will time out after 75 seconds. The server sends a total of 10 such probes with an interval of 75 seconds each. If the server does not receive a response, it assumes that the client host has closed and terminated the connection.
The customer host crashed and restarted. The server will receive a response to its inactive probe, which is a reset that causes the server to terminate the connection. The client is running normally, but the server is unreachable, which is similar to 2. All TCP can find is that there is no response to the probe.
3.4 long connection and short connection operation process
The operation procedure of a short connection is: establish a connection-- data transfer-- close the connection. Establish connection-data transfer-close connection
The operation procedure of a long connection is: establish a connection-data transmission. (keep the connection). Data transfer-close the connection
The diagram is as follows:
4. Advantages and disadvantages of long and short connections
As can be seen from the above, long connections can save more TCP establishment and closing operations, reduce waste and save time. For customers who frequently request resources, it is more suitable for long connections. However, there is a problem here. The detection cycle of the survival feature is too long, and it is a relatively gentle way to detect the survival of TCP connections. When you encounter malicious connections, the survival feature is not enough. In the application scenario of persistent connections, the client usually does not actively close the connection between them. If the connection between Client and server is not closed all the time, there will be a problem. As there are more and more client connections, server will be unable to handle it sooner or later. At this time, the server side needs to adopt some strategies, such as closing some connections that have not been read or written for a long time. This can prevent some malicious connections from causing damage to server-side services. If the conditions permit, you can take the client machine as the granularity and limit the maximum number of long connections per client, which can completely prevent a painful client from affecting the back-end service.
Short connections are relatively simple to manage for the server, and existing connections are useful connections that do not require additional control. However, if the customer requests frequently, it will waste time and bandwidth on the establishment and shutdown of the TCP.
The generation of long connection and short connection lies in the shutdown strategy adopted by client and server, the specific application scenario adopts specific strategy, there is no perfect choice, only the appropriate choice.
Summary: the advantages of long links are the disadvantages of short links, and vice versa.
5. When to use long connection, short connection?
Long connections are mostly used for frequent operations, point-to-point communication, and the number of connections should not be too many. Each TCP connection requires a three-step handshake, which takes time. If each operation is connected first, then the processing speed will be much lower, so it will continue to open after each operation, and the OK will be sent directly during the second processing. There is no need to establish a TCP connection. For example, database connections use long connections, frequent communication with short connections will cause socket errors, and frequent socket creation is also a waste of resources.
Http services like WEB sites generally use short links, because long links will consume certain resources for servers, while thousands or even hundreds of millions of clients with short connections like WEB sites will save some resources. If long links are used, and there are thousands of users at the same time, it can be imagined if each user occupies a connection. Therefore, the amount of concurrency is large, but each user needs to use short connection when there is no need for frequent operation.
Thank you for your reading, the above is the content of "how to understand the long connection and short connection of HTTP". After the study of this article, I believe you have a deeper understanding of how to understand the long connection and short connection of HTTP. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.