HTTP long connection, short connection, long polling, the short polling HTTP long connection, what exactly is a short connection?

1. The previous misunderstanding

I heard a long time before saying too long connection, but also know HTTP1.0 protocol does not support long connection, From HTTP1.1 agreement, default connection is a long connection. But in the end felt for a long connection has been muddle, a kind of feeling can not find the key.

Today, through some research, and finally understand the mystery of it. Previously, we have seen long connection-related content, but has been foggy. This has been able to figure out in such a short period of time, and settling himself technologies are inseparable. So, here take this opportunity to emphasize once again, do not try to study something you studied for a long time are whole do not understand, perhaps less than your level, or maybe you've never been in contact with the actual application scenarios In this case you go to research, only half done, nothing vain about it.

Back to the topic, since that is misunderstood, so misunderstood in the end is what?

That's always thought, HTTP connection is divided into short and long connection connected, and we are now commonly used HTTP1.1, therefore we are a long connection.

In fact, this sentence is only half right, we now HTTP protocol, mostly 1.1, so we usually use are basically long connection. But first phrase is wrong, HTTP protocol did not say that the length of the connection, but also because of this misunderstanding, leading to a long connection has been unclear, not always its essentials, we will discuss some concrete below.

Many articles on the web are fraught did not understand that concept. Here we must emphasize, HTTP protocol is based on request / response mode, so as long as the server to the response, this HTTP connection is over, or, more precisely, is this the HTTP request is over, there is no longer connected With that said. So naturally there is no short connecting this to say.

The reason why the network is divided into long HTTP connections and short connections, in fact, essentially said TCP connection. TCP connection is a two-way channel, it can be maintained for a period of time does not close, so TCP connections have real short and long connection connecting the said.

In fact, knew what was coming, you will find this is well understood. HTTP protocol application layer protocol in the final analysis, while the TCP transport layer protocol is the real and only responsible for the transmission of this layer only needs to establish a connection.

One example is the image, take your online shopping for, HTTP protocol means that express orders, fill in your mailing list is like when an HTTP request is made, and other goods to the place, the courier will according to your request to send the goods to the consignee accordingly. The TCP protocol is the middle of the big cargo truck, train or plane it may be, but no matter what, it is responsible for transport, and therefore must have a road, whether on the ground or the sky. Well, this road is called TCP connection, which is a bi-directional data channel.

Therefore, LZ and even now feel, "HTTP connection" is the word should not appear, it's just an application layer protocol, there is no so-called connection that said, like FTP protocol is the application layer, but you have heard over FTP connections? (Well, it seems that heard -_-, but now you know, in fact, the so-called FTP connection, strictly speaking, is still a TCP connection)

Indeed, said HTTP request and HTTP response would be more accurate, the HTTP request and HTTP response, a TCP connection this channel is transmitted back and forth.

 

不管怎么说,一定要务必记住,长连接是指的TCP连接,而不是HTTP连接。

 

A question

Before LZ has been somewhat confusing one thing, even if the first is how to become a long HTTP connection, is not just set Connection to a keep-alive even?

If yes, then say HTTP1.1 default connection is long, but we usually observe the HTTP header developing Web applications, Connection indeed is keep-alive, that is to say most of them are long connection we use, but long connections are not generally used to interact more frequent applications? Like us ordinary Web applications, such as garden this blog, or my personal blog this long connection with what?

If the use is useful in the end what it is that we are not the client and server applications that interact frequently (after all, you certainly have to open the page a long time to open another bar), if useless, then in the end should not be to keep the Connection -alive to get rid of this header value, and thus into a short connection?

This question, in fact, LZ understand the long connection means after the TCP connection, basically understand. And this question, it is also LZ in the "previous misunderstanding" that period mentioned, that misunderstanding has led to LZ do not understand the question.

Why then solves the above misunderstanding, said before these questions LZ understand everything?

Because of the long connection means that the connection can be reused, after all, it has maintained a connection not just to re-use thing. But if long connection means of HTTP, then it means that HTTP connection can be reused, if this sounds awkward situation. The reason feel uncomfortable, actually a kind of intuition LZ, nothing theoretical basis. The source of this awkward is that, until there is no feeling of mastery, so always feel something is missing. But this doubt, did not affect the LZ work, and therefore no get to the bottom too.

But now, to understand the long connection actually refers to TCP connections, LZ himself instantly wanted to understand those issues above.

The first question is, is not just set to Connection keep-alive even longer connected?

Of course it is, but to set up both the server and client.

The second problem is that we usually use is not long connection?

This is also no doubt, of course, yes. (Basically HTTP1.1 agreement now using, you look you will find, are basically Connection keep-alive. And on the HTTP protocol document also mentioned, HTTP1.1 default connection is long, which is the default Connection the value is the keep-alive)

The third question, before most LZ also do not understand the problem, and that is that we ordinary Web applications (such as blog garden, my personal blog this) with long connections so what are the benefits? Or need to switch off long connection using a short connection?

This problem LZ now finally understand, the answer is some good news.

What are the benefits?

First of all, I have already said, a long connection to reuse, before the LZ will understand. Since that long connection is a TCP connection, that reuse is a TCP connection. It is much easier to explain, say, in the long connections, multiple HTTP request can reuse the same TCP connection, which saves consume a lot of TCP connection establishment and disconnection.

For example, you request a web page blog garden, there is certainly this page also includes CSS, JS and so a series of resources, if you are short connection (that is, each time to re-establish a TCP connection), then every time you open a web pages, basic to establish several or even dozens of TCP connections, which do not have the amount of resources wasted LZ to say it.

But if it is a long connection, then so many HTTP requests (these requests, including requests Web content, CSS files, JS files, pictures, etc.), are actually using a TCP connection, it is clear that you can save a lot of consumption.

As a result interpretation, it is clear, do not know how to explain the feeling you read, anyway, I want to understand LZ in the future, suddenly a kind of feeling.

In addition, the last on a long connection even more the way, that is, long connection is not permanently connected. If the period of time (the specific length of time, can be set in the header of which, the so-called timeout), the HTTP request, then the connection is not issued, then the length of the connection will be broken.

This is actually very easy to understand, otherwise, TCP connection will be more and more, until the number of TCP connection to the server to explode to the upper limit. Now think about it, for the server, the server where the connection is actually very long taste database connection pool, we are all connected reuse in order to save thing, right?

Long and short polling polling

LZ has put in front of basically almost the length of said connection, then talk about the length of the polling, today precisely in order to study the length of the polling, LZ was under way to study the length of the connection thing.

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, and maintaining servers in the same physical inventory.

This time, how would you do?

The easiest one way that you write with JS death cycle, stop to ask for inventory server is how much, then refresh this page to them, this is in fact the so-called short polling.

This approach has obvious disadvantages, that is, you're a waste of resources and client server. The client better point, now high PC machine configuration, you keep the request is still not the user's computer to death, but the server is very egg pain. If there are 1,000 people remain in a product detail page, that means there will be 1000 client requests the server to stop acquiring inventory, which is obviously unreasonable.

then what should we do?

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.

As for the client, whether it is long or short poll to poll, the client's actions are the same, it is to keep to the request, except that the service side, under circumstances short polling the server for every request no matter no change will immediately return the results, and the next long polling, if there is a change would immediately return a result, and there is no change, then the client will not immediately return a result, until the timeout.

As a result, the number of requests the client will be a significant reduction (which means savings in network traffic, after all, every request made, traffic will occupy download the client and server upload traffic), but also to solve the server has been exhausted to accept the request of the dilemma.

But the long polling is a bad thing, because the same request pending would lead to a waste of resources, assuming that 1,000 people still remain in some commodity details page, it is likely the server side threads hanging from 1000, kept detection of inventory, this is still a problem.

Thus, it can be seen here, whether short or long polling polling, are much less suitable for the case where the number of clients, since the server TCP connections per can carry is capped, this wheel Inquiry is easy to connect several top full. The reason For example, just because I am sure you are online shopping, so the more popular examples of that.

Even polling can not solve this problem acquiring inventory, but as long as we understand the difference between the length of the polling, it was enough. In fact, it was observed that LZ their daily shopping, the inventory should not change, this example is purely personal LZ obscenity, -_-.

Polling and the length difference between the length of the connecting

Here briefly about the difference between them, LZ just say the most fundamental difference here.

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.

 

HTTP long connection, what exactly is a short connection?

 

Relations 1. HTTP protocol and TCP / IP protocol

HTTP long connections and short connection essentially TCP connections and short length connector. HTTP is an application layer protocol, using the TCP protocol in the transport layer, IP at the network layer. IP network routing protocol and addressing mainly to solve the problem, TCP protocol is mainly to solve how to reliably transfer data packets over the IP layer, so that the receiver receives all packets sent by the transmitting end of the network, and is consistent with the order of transmission order. TCP protocol is reliable, connection-oriented.

2. How to understand the HTTP protocol is stateless

HTTP protocol is stateless, referring to the agreement for the transaction no memory capacity, the server does not know what the client state. That is to say, open a Web page on a server and open the last time there is no link between the pages on this server. HTTP is a stateless connection-oriented protocol, does not mean stateless HTTP TCP connection can not be maintained, but can not represent the HTTP protocol uses the UDP (no connection).

3. What are the long connection, short connection?

Default short connection in HTTP / 1.0 in. In other words, the client and the server once for each HTTP operation, once the connection is established, the task will end disconnected. When an HTML Web page or other type of client browser to access the Web contains other resources (such as JavaScript files, image files, CSS files, etc.), when confronted with such a Web resource, the browser will be re-established an HTTP session.

From the HTTP / 1.1 cases, long default connection for holding the connection characteristics. A long connection using the HTTP protocol, which will be added in response to the first line of code:

Connection:keep-alive

In the case when TCP long connection, when a page is opened is completed, for the transmission of HTTP data between the client and the server connection is not closed, clients access the server again, we will continue to use a connection has been established . Keep-Alive not permanently remain connected, it has a hold time, may be set at this time to a different server software (e.g., Apache) in. Achieve a long connection requires a client and server support long connection.

Long and short connection protocol HTTP connection, the connection is substantially long and short protocol TCP connections.

3.1. TCP connection

When using TCP protocol network traffic, before the actual read and write operations, you must establish a connection between the client and server side, when the write operation is completed, the two sides no longer need this connection can be released in this connection. To establish a connection rely on "three-way handshake", release the need to "handshake" so that each is required to establish a connection resource consumption and time consumption.

The classic three-way handshake to establish connection diagram:

The classic four-way handshake close connection diagram:

3.2. TCP short connection

Simulation about the situation of short TCP connections: client initiates a connection request to the server, server receives the request, then the two sides to establish a connection. client sends a message to the server, server response to client, then the first request is completed. This time both sides can initiate close any operation, but these are generally client to initiate a close operation. Foregoing, short connection request operation usually only one transfer between the client / server.

Advantage of short connection are: management is relatively simple, existing connections are useful connection, no additional control.

3.3. TCP connection length

Let us look at the situation long simulation connection: client initiates a connection to the server, server to accept client connection, the two sides establish a connection, client and server after the completion of the first request, the connection between them and will not take the initiative to shut down, subsequent read and write operations will continue use this connection.

TCP keep-alive function mainly provides for server applications. If the client has disappeared and the connection is not disconnected, so that will keep a half-open connections on the server, and the server and is waiting for data from the client, then the server will always wait for the client's data. Keepalive function is to try to detect the server end of this half-open connection.

If a given connection without any action within two hours, the server sends to the client a packet detecting section, in response to detecting four client state based on the client host:

  • Client host is still running, and the server is reachable. At this time, the TCP client response, the server will keep alive timer is reset.
  • Client host has crashed and is being shut down or restart. TCP client can respond under the above circumstances. The server will not receive a response to the detection of the client. A total of 10 transmission server such detection, each spacer 75 seconds. If the server does not receive any response, it assumes that the client is closed and terminates the connection.
  • 客户端崩溃并已经重新启动。服务器将收到一个对其保活探测的响应,这个响应是一个复位,使得服务器终止这个连接。
  • 客户机正常运行,但是服务器不可达。这种情况与第二种状态类似。

4. 长连接和短连接的优点和缺点

由上可以看出,长连接可以省去较多的TCP建立和关闭的操作,减少浪费,节约时间。对于频繁请求资源的客户端适合使用长连接。在长连接的应用场景下,client端一般不会主动关闭连接,当client与server之间的连接一直不关闭,随着客户端连接越来越多,server会保持过多连接。这时候server端需要采取一些策略,如关闭一些长时间没有请求发生的连接,这样可以避免一些恶意连接导致server端服务受损;如果条件允许则可以限制每个客户端的最大长连接数,这样可以完全避免恶意的客户端拖垮整体后端服务。

短连接对于服务器来说管理较为简单,存在的连接都是有用的连接,不需要额外的控制手段。但如果客户请求频繁,将在TCP的建立和关闭操作上浪费较多时间和带宽。

长连接和短连接的产生在于client和server采取的关闭策略。不同的应用场景适合采用不同的策略。

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

短连接对于服务器来说管理较为简单,存在的连接都是有用的连接,不需要额外的控制手段。但如果客户请求频繁,将在TCP的建立和关闭操作上浪费时间和带宽

长连接和短连接的产生在于client和server采取的关闭策略,具体的应用场景采用具体的策略,没有十全十美的选择,只有合适的选择。

长连接短连接操作过程

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

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

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

 

http和socket之长连接和短连接区别

http://www.jianshu.com/p/b68d2b26f5f4

 

HTTP 的长连接和短连接

http://blog.jobbole.com/104108/

 

HTTP持久连接

https://zh.wikipedia.org/wiki/HTTP%E6%8C%81%E4%B9%85%E8%BF%9E%E6%8E%A5

 

长连接

http://baike.baidu.com/view/2831907.htm

1. HTTP协议与TCP/IP协议的关系

HTTP的长连接和短连接本质上是TCP长连接和短连接。HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议。 IP协议主要解决网络路由和寻址问题,TCP协议主要解决如何在IP层之上可靠地传递数据包,使得网络上接收端收到发送端所发出的所有包,并且顺序与发送顺序一致。TCP协议是可靠的、面向连接的。

2. 如何理解HTTP协议是无状态的

HTTP协议是无状态的,指的是协议对于事务处理没有记忆能力,服务器不知道客户端是什么状态。也就是说,打开一个服务器上的网页和上一次打开这个服务器上的网页之间没有任何联系。HTTP是一个无状态的面向连接的协议,无状态不代表HTTP不能保持TCP连接,更不能代表HTTP使用的是UDP协议(无连接)。

3. 什么是长连接、短连接?

在HTTP/1.0中默认使用短连接。也就是说,客户端和服务器每进行一次HTTP操作,就建立一次连接,任务结束就中断连接。当客户端浏览器访问的某个HTML或其他类型的Web页中包含有其他的Web资源(如JavaScript文件、图像文件、CSS文件等),每遇到这样一个Web资源,浏览器就会重新建立一个HTTP会话。

而从HTTP/1.1起,默认使用长连接,用以保持连接特性。使用长连接的HTTP协议,会在响应头加入这行代码:

Connection:keep-alive

在使用长连接的情况下,当一个网页打开完成后,客户端和服务器之间用于传输HTTP数据的TCP连接不会关闭,客户端再次访问这个服务器时,会继续使用这一条已经建立的连接。Keep-Alive不会永久保持连接,它有一个保持时间,可以在不同的服务器软件(如Apache)中设定这个时间。实现长连接需要客户端和服务端都支持长连接。

HTTP协议的长连接和短连接,实质上是TCP协议的长连接和短连接。

3.1. TCP连接

当网络通信时采用TCP协议时,在真正的读写操作之前,客户端与服务器端之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时可以释放这个连接。连接的建立依靠“三次握手”,而释放则需要“四次握手”,所以每个连接的建立都是需要资源消耗和时间消耗的。

经典的三次握手建立连接示意图:

经典的四次握手关闭连接示意图:

3.2. TCP短连接

模拟一下TCP短连接的情况:client向server发起连接请求,server接到请求,然后双方建立连接。client向server发送消息,server回应client,然后一次请求就完成了。这时候双方任意都可以发起close操作,不过一般都是client先发起close操作。上述可知,短连接一般只会在 client/server间传递一次请求操作。

短连接的优点是:管理起来比较简单,存在的连接都是有用的连接,不需要额外的控制手段。

3.3. TCP长连接

我们再模拟一下长连接的情况:client向server发起连接,server接受client连接,双方建立连接,client与server完成一次请求后,它们之间的连接并不会主动关闭,后续的读写操作会继续使用这个连接。

TCP的保活功能主要为服务器应用提供。如果客户端已经消失而连接未断开,则会使得服务器上保留一个半开放的连接,而服务器又在等待来自客户端的数据,此时服务器将永远等待客户端的数据。保活功能就是试图在服务端器端检测到这种半开放的连接。

如果一个给定的连接在两小时内没有任何动作,服务器就向客户发送一个探测报文段,根据客户端主机响应探测4个客户端状态:

  • 客户主机依然正常运行,且服务器可达。此时客户的TCP响应正常,服务器将保活定时器复位。
  • 客户主机已经崩溃,并且关闭或者正在重新启动。上述情况下客户端都不能响应TCP。服务端将无法收到客户端对探测的响应。服务器总共发送10个这样的探测,每个间隔75秒。若服务器没有收到任何一个响应,它就认为客户端已经关闭并终止连接。
  • 客户端崩溃并已经重新启动。服务器将收到一个对其保活探测的响应,这个响应是一个复位,使得服务器终止这个连接。
  • 客户机正常运行,但是服务器不可达。这种情况与第二种状态类似。

4. 长连接和短连接的优点和缺点

由上可以看出,长连接可以省去较多的TCP建立和关闭的操作,减少浪费,节约时间。对于频繁请求资源的客户端适合使用长连接。在长连接的应用场景下,client端一般不会主动关闭连接,当client与server之间的连接一直不关闭,随着客户端连接越来越多,server会保持过多连接。这时候server端需要采取一些策略,如关闭一些长时间没有请求发生的连接,这样可以避免一些恶意连接导致server端服务受损;如果条件允许则可以限制每个客户端的最大长连接数,这样可以完全避免恶意的客户端拖垮整体后端服务。

短连接对于服务器来说管理较为简单,存在的连接都是有用的连接,不需要额外的控制手段。但如果客户请求频繁,将在TCP的建立和关闭操作上浪费较多时间和带宽。

长连接和短连接的产生在于client和server采取的关闭策略。不同的应用场景适合采用不同的策略。

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

短连接对于服务器来说管理较为简单,存在的连接都是有用的连接,不需要额外的控制手段。但如果客户请求频繁,将在TCP的建立和关闭操作上浪费时间和带宽

长连接和短连接的产生在于client和server采取的关闭策略,具体的应用场景采用具体的策略,没有十全十美的选择,只有合适的选择。

长连接短连接操作过程

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

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

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

 

http和socket之长连接和短连接区别

http://www.jianshu.com/p/b68d2b26f5f4

 

HTTP 的长连接和短连接

http://blog.jobbole.com/104108/

 

HTTP持久连接

https://zh.wikipedia.org/wiki/HTTP%E6%8C%81%E4%B9%85%E8%BF%9E%E6%8E%A5

 

长连接

http://baike.baidu.com/view/2831907.htm

Guess you like

Origin www.cnblogs.com/cnndevelop/p/12188048.html