Talking about HTTP protocol (1) TCP long connection

previous knowledge


When I first came into contact with HTTP requests, I heard that there are two versions of HTTP requests, 1.0 and 1.1 ( in fact, there is also a 0.9 version, because only GET is accepted and the POST method is not supported, so the client cannot pass too much information to the server. People ignore it ), and also know that the HTTP1.0 protocol does not support long-term connections, since the HTTP1.1 protocol, the connection is a long-term connection by default. But after all, I feel that I have been ignorant of long connections, and I feel that I can't grasp the key points.


We are all using HTTP1.1 version, and HTTP1.1 version supports long-term connection (also called persistent connection), so we usually use long-term connection, I always thought so. As a result of recent exploration, this is not the case. Because the HTTP protocol does not have a long and short connection at all.


As we all know, the HTTP protocol is based on the request/response mode, so as long as the server gives a response, this HTTP connection ends, or more precisely, this HTTP request ends, and the next time is a new one requests and new responses, so there is no such thing as a long connection at all. So naturally there is no such .


The reason why HTTP is divided into long connections and short connections on the Internet is actually a TCP connection. A TCP connection is a two-way channel, and it can be kept open for a period of time , so the TCP connection has a real long connection and a short connection. The HTTP protocol is an application layer protocol in the final analysis, while TCP is the real transport layer protocolresponsible for transmission needs to establish a connection.


Therefore, the concept of "HTTP connection" should not appear at all. HTTP is only an application layer protocol, and there is no such thing as a connection at all. Just like the FTP protocol , we will never say "FTP connection". In the final analysis, in fact, the connection is a TCP connection only at the transport layer. On the contrary, it are transmitted through the data channel of TCP connection to transmit requests and responses.


Speaking of this, the previous misunderstanding has been completely changed. In the future, remember that long connections and short connections refer to the TCP connection of the transport layer, not the HTTP protocol of the application layer. HTTP long and short connections are essentially TCP long and short connections. HTTP belongs to the application layer protocol, uses the TCP protocol at the transport layer, and uses the IP protocol at the network layer. The IP protocol mainly solves the problem of network routing and addressing, and the TCP protocol mainly solves how to reliably deliver data packets above the IP layer, so that the other end on the network receives all , and the order is consistent with the sending order. TCP is reliable, connection-oriented.


current question

Now that we talk about long connections, what are long connections and short connections? How to realize long connection in HTTP1.1? So what are the advantages and disadvantages of long and short connections ? Just as when we learn a new knowledge, we always ask ourselves these three questions: What is XXX? How to use XXX? The benefits of XXX? Like ourordinary web application, csdnblogging platform, what is the use of this long connection?


1. How to understand that the HTTP protocol is stateless

The HTTP protocol is stateless, which means that the protocol has no memory capability for transaction processing, and the server does not know what state the client 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. Statelessness does not mean that HTTP cannotmaintain a TCP connection, nor does


2. What is a long connection and a short connection?

In HTTP/1.0, short connections are used by default . That is to say, every time the browser and the server perform an HTTP operation, a connection is established, but the connection is terminated when the task ends. If a certain 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.; when the browser encounters such a Web resource, it will create a HTTP session. But since  HTTP/1.1, persistent connections are used by default to maintain connection characteristics. Using the HTTP protocol with a long connection, this line of code will be added to the response header:

Connection:keep-alive server and client must be set

In the case of using a long 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. If the client accesses the web page on the server again, it will continue to use this one established connection. Keep-Alive does not keep the connection forever, it has a keep time, which can be set in different server software (such as Apache). To implement persistent connections, both the client and the server must support persistent connections. The long connection and short connection of the HTTP protocol are essentially the long connection and short connection of the TCP protocol.

3. TCP connection

When the TCP protocol is used for network communication, a connection must be established between the server and the client before the real read and write operations. When the read and write operations are completed, the two parties can release the connection when they no longer need the connection. It requires three handshakes, and release requires four handshakes, so the establishment of each connection requires resource consumption and time consumption.

Classic three-way handshake diagram:

经典的四次分手关闭图:



4,TCP短连接

我们模拟一下TCP短连接的情况,client向server发起连接请求,server接到请求,然后双方建立连接。client向server 发送消息,server回应client,然后一次读写就完成了,这时候双方任何一个都可以发起close操作,不过一般都是client先发起 close操作。为什么呢,一般的server不会回复完client后立即关闭连接的,当然不排除有特殊的情况。从上面的描述看,短连接一般只会在 client/server间传递一次读写操作。

短连接的操作步骤是:建立连接——数据传输——关闭连接…建立连接——数据传输——关闭连接


5,TCP长连接

接下来我们再模拟一下长连接的情况,client向server发起连接,server接受client连接,双方建立连接。Client与server完成一次读写之后,它们之间的连接并不会主动关闭,后续的读写操作会继续使用这个连接。比如你请求了csdn的一个网页,这个网页里肯定还包含了CSS、JS等等一系列资源,如果你是短连接(也就是每次都要重新建立TCP连接)的话,那你每打开一个网页,基本要建立几个甚至几十个TCP连接,但如果是长连接的话,那么这么多次HTTP请求(这些请求包括请求网页内容,CSS文件,JS文件,图片等等),其实使用的都是一个TCP连接,很显然是可以节省很多消耗的。另外,最后关于长连接还要多提一句,那就是,长连接并不是永久连接的。如果一段时间内(具体的时间长短,是可以在header当中进行设置的,也就是所谓的超时时间),这个连接没有HTTP请求发出的话,那么这个长连接就会被断掉。

这一点其实很容易理解,否则的话,TCP连接将会越来越多,直到把服务器的TCP连接数量撑爆到上限为止。现在想想,对于服务器来说,服务器里的这些个长连接其实很有数据库连接池的味道,大家都是为了节省连接重复利用嘛,对不对?

长连接的操作步骤是:建立连接——数据传输…(保持连接)…数据传输——关闭连接


6,长连接和短连接的优点和缺点

由上可以看出,长连接可以省去较多的TCP建立和关闭的操作,减少浪费,节约时间。对于频繁请求资源的客户来说,较适用长连接。不过这里存在一个问题存活功能的探测周期太长,还有就是它只是探测TCP连接的存活,属于比较斯文的做法,遇到恶意的连接时,保活功能就不够使了。在长连接的应用场景下,client端一般不会主动关闭它们之间的连接,Client与server之间的连接如果一直不关闭的话,会存在一个问题,随着客户端连接越来越多,server早晚有扛不住的时候,这时候server端需要采取一些策略,如关闭一些长时间没有读写事件发生的连接,这样可 以避免一些恶意连接导致server端服务受损;如果条件再允许就可以以客户端机器为颗粒度,限制每个客户端的最大长连接数,这样可以完全避免某个蛋疼的客户端连累后端服务。短连接对于服务器来说管理较为简单,存在的连接都是有用的连接,不需要额外的控制手段。但如果客户请求频繁,将在TCP的建立和关闭操作上浪费时间和带宽


7,什么时候用长连接,短连接

长连接多用于操作频繁,点对点的通讯,而且连接数不能太多情况,。每个TCP连接都需要三步握手,这需要时间,如果每个操作都是先连接,再操作的话那么处理速度会降低很多,所以每个操作完后都不断开,次处理时直接发送数据包就OK了,不用建立TCP连接。例如:数据库的连接用长连接, 如果用短连接频繁的通信会造成socket错误,而且频繁的socket 创建也是对资源的浪费。而像WEB网站的http服务一般都用短链接,因为长连接对于服务端来说会耗费一定的资源,而像WEB网站这么频繁的成千上万甚至上亿客户端的连接用短连接会更省一些资源,如果用长连接,而且同时有成千上万的用户,如果每个用户都占用一个连接的话,那可想而知吧。所以并发量大,但每个用户无需频繁操作情况下需用短连好。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326118095&siteId=291194637