In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the long connection and short connection between HTTP and TCP". The content 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 "what is the long connection and short connection between HTTP and TCP".
The relationship between 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.
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).
What are long connections and short connections?
Short connection
Connect-> transfer data-> close the connection
HTTP is stateless, and every time the browser and server perform a HTTP operation, a connection is established, but the connection is interrupted at the end of the task.
It can also be said that a short connection refers to the disconnection immediately after the Socket connection is sent and the data is received.
Long connection
Connect-> transfer data-> stay connected-> transfer data->. -> close the connection.
A persistent connection means that a Socket connection is maintained after it is established, regardless of whether it is used or not.
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.
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, both sides no longer need the connection, 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.
A classic three-way handshake:
Classic four-way handshake closing picture:
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.
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.
Operation procedure of long connection and short connection
The steps for a short connection are:
Establish a connection-data transfer-close the connection. Establish connection-data transfer-close connection
The procedure for a long connection is:
Establish a connection-data transmission. (stay connected). Data transfer-close the connection
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 side generally 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, the 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 occurred 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.
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.
How long is the life cycle of long and short connections?
After a short connection is established, a read and write is completed and then it is automatically closed.
Normally, after a TCP persistent connection is established, as long as the pair does not make a request to close it and there is no exception, the connection will not be closed automatically by the operating system, and it can still be used even after a change in the physical network topology. So it is possible that a connection can be maintained for days, months, years or more, as long as there is no exception or the user (application layer) actively closes it.
In programming, it is often necessary to establish a TCP connection and stay connected for a long time. There is no exact time limit for the so-called TCP persistent connection, but it takes a long time for this connection.
How to detect whether the persistent connection is broken?
1. Use heartbeat in the application layer to actively detect
For the network communication programs with high real-time requirements, it is often necessary to obtain the interrupted connection in a more timely manner, so as to deal with it in time. However, if the other party's connection is abnormally interrupted, it is often unable to get the information that the other party's connection has been interrupted in time, and the time interval for the operating system to detect whether the connection is interrupted is relatively long by default, even if it can be detected. but it does not meet our real-time requirements, so we need to detect it manually.
2. Change the keepalive option of socket to make the time interval for socket to detect whether the connection is interrupted or not, so as to meet our timeliness needs. Several related options are used and parsed as follows:
A. when we detect that the peer is disconnected in an unelegant way, we can set the SO_KEEPALIVE property so that we can find out whether the other party's TCP connection still exists after 2 hours. The usage is as follows:
KeepAlive = 1 setsockopt (listenfd, SOL_SOCKET, SO_KEEPALIVE, (void*) & keepAlive, sizeof (keepAlive))
B. If we do not want to use such a long waiting time, we can modify the configuration parameters of the kernel on the network side, or we can set the TCP layer (SOL_TCP) options of SOCKET TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT.
TCP_KEEPIDLE: TCP idle time before the start of the first KeepAlive probe (default 2 hours)
TCP_KEEPINTVL: the interval between two KeepAlive probes (default 75s)
TCP_KEEPCNT: number of KeepAlive probes before disconnection
If the heartbeat function is to maintain the survival of the client, that is, the server must send a certain amount of data to the client segment at regular intervals, then the use of SO_KEEPALIVE is very inadequate. Because the SO_KEEPALIVE option means that "there is no data exchange in either direction of this interface." On the Linux 2.6series, the above understanding is that as long as the socket that turns on the SO_KEEPALIVE option detects data transmission or data acceptance, it is considered a data exchange. Therefore, in this case, it is completely useless to use the SO_KEEPALIVE option to detect whether the other party is not connected properly, and it is impossible for keep-alive packets to be sent at regular intervals. The upper layer program can send packets to the buffer normally in the case of abnormal disconnection. An abnormal open condition means that the server does not receive a "FIN" or "RST" packet.
Thank you for your reading, the above is "what is the long connection and short connection between HTTP and TCP". After the study of this article, I believe you have a deeper understanding of what the long connection and short connection between HTTP and TCP are, and the specific use needs to be verified in practice. 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.