【Turn】Long connection and short connection


First, let's introduce the difference between short links and long connections:
short connection

connection -> transfer data -> close the connection. For
example , HTTP is a stateless short link. Every time the browser and the server perform an HTTP operation, a connection is established, but the task ends. disconnect.
Specifically, the browser client initiates and establishes a TCP connection -> the client sends the HttpRequest message -> the server receives the message -> the server handle sends the HttpResponse message to the front end, and immediately calls the socket.close method after sending
-> the client receives the response The message -> client will eventually receive a signal that the server disconnects the TCP connection -> the client disconnects the TCP connection, specifically by calling the close method.

It can also be said that a short connection means that after the SOCKET is connected, the connection is immediately disconnected after sending and receiving data.
Because the connection is disconnected after receiving the data, there will be no connection every time the data is received and processed. This is one of the reasons why the HTTP protocol is stateless.

Long connection
connection -> transfer data -> keep connection -> transfer data -> ........... -> until one party closes the connection, mostly the client closes the connection.
A long connection means that the connection is maintained regardless of whether it is used or not after the SOCKET connection is established, but the security is poor.

The choice of HTTP on short and long connections:

HTTP is stateless, that is, 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

Compared with HTTP1.0, the biggest difference is the addition of persistent connection support (it seems that the latest HTTP1.1 can display the specified keep-alive), but it is still stateless, or can not be trusted .
If the browser or server adds the line Connection:keep-alive to its header, the
TCP connection will remain open after it is sent, so the browser can continue to send requests over the same connection. Keeping connected saves the time required to establish a new connection for each request and also saves bandwidth.
To implement persistent connections, both the client and the server must support persistent connections.

When to use long connection, short connection?
Long connections are mostly used for frequent operations, point-to-point communication, and the number of connections cannot be too many. Each TCP connection requires a three-step handshake, which takes time. If each operation is connected first and then operated, the processing speed will be greatly reduced, so each operation will not be disconnected after each operation, and data packets will be sent directly during the next processing. OK, no need to establish a TCP connection. For example, long connections are used for database connections. If short connections are used for frequent communication, socket errors will occur, and frequent socket creation is also a waste of resources.

And http services like WEB sites generally use short links, because long connections will consume a certain amount of resources for the server, and short links will be more economical for the connection of thousands or even hundreds of millions of clients as frequently as WEB sites. For some resources, if you use a long connection and there are thousands of users at the same time, if each user occupies a connection, then you can imagine it. Therefore, the amount of concurrency is large, but it is better to use a short connection if each user does not need to operate frequently.

In short, the choice of long and short connections depends on the situation.

For specific network applications:
http 1.0一般就指短连接,smtp,pop3,telnet这种就可以认为是长连接。一般的网络游戏应用都是长连接

Guess you like

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