Http long connections and short connections

Source: Internet
Author: User

HTTP1.1 Specifies that long connections are maintained by default (HTTP persistent connection, also translated as persistent connections), the data transfer is done to keep the TCP connection continuously open (no RST packet, no four handshake), Wait for the same domain name to continue to use this channel to transmit data , the opposite is the short connection.

Long connection (this link can remain 20s):

If the server does not tell the client time-out is OK, the service may initiate four handshake disconnection TCP connection, the client can know that the TCP connection is invalid, and TCP also has a heartbeat packet to detect whether the current connection is still alive, many ways to avoid wasting resources.

TCP/IP, Http, SOCKET, long connection, short connection

What is TCP/IP?

TCP/IP is a protocol group that can be divided into three levels: Network layer, Transport layer and application layer.
At the network layer are IP protocols, ICMP protocols, ARP protocols, RARP protocols, and BOOTP protocols.
There are TCP protocols and UDP protocols in the transport layer.
In the application layer, there are FTP, HTTP, TELNET, SMTP, DNS and other protocols.

What is a socket?

Socket是应用层与TCP/IP协议族通信的中间软件抽象层,一组接口,把复杂的TCP/IP协议族隐藏在Socket接口后面。

So HTTP is the protocol for the TCP/IP application layer. Sockets are their software abstraction layer.

Short connections:
Connection, transfer data, close connection
HTTP is stateless, and the browser and server make a connection every time an HTTP operation is made, but the end of the task interrupts the connection.
It is also possible to say that a short connection is when the socket connection is sent and the data is disconnected immediately after it is received.
Long connections:
Connect, transfer data--keep connected, transfer data ... , close the connection.
A long connection is when a socket connection is established, regardless of whether it is used or not, but is less secure.
Long connections for http:
HTTP can also be established for long connections, using Connection:keep-alive,http 1.1 to make persistent connections by default. The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections (appearance
Like the newest http1.0 can display the specified keep-alive), but still stateless, or can not be trusted.

Image description of Socket protocol
The English literal of the socket is "hole" or "socket". Here as a 4BDS UNIX process communication mechanism, take the latter meaning. The socket is very similar to a telephone socket. Take a national telephone network as an example. Telephone calls on both sides of the equivalent of communication between the 2 processes, the area code is its network address, a unit in the zone of the switch equivalent to a host, the host assigned to each user's internal number equivalent to the socket number. Any user before the call, the first to occupy a telephone, equivalent to apply for a socket, and to know the other person's number, the equivalent of the other side has a fixed socket. Then dial the call to the other side, the equivalent of making a connection request (if the other party is not in the same area, also dial the other area code, equivalent to the network address). If the other party is present and idle (equivalent to the communication of another host to boot and can accept the connection request), pick up the telephone handset, the two sides can formally call, the equivalent of a successful connection. The process of the two sides of the call is the direction of the telephone signal and the other side from the telephone to receive the signal, the equivalent of sending data to the socket and receiving data from the socket. After the call is over, one side hangs up the phone equivalent to closing the socket and undoing the connection.
This is usually the case with short connections: Connect, transfer data, close the connection

So what is a long connection? Generally long connections are relative to short connections, long connections do not close the connection after the number of transfers, and the constant sending packet keeps the connection waiting for the next packet to be processed.
such as: transmission data--transmission data--... , close the connection .

When do you use short connections?
The general long connection is used for the frequent communication of a few client-end to Server-end, for example: the connection of the database with long connection, if the communication with the short connection frequently causes the socket error, and the frequent socket creation is also a waste of resources.
HTTP services like Web sites typically use short links, because long connections can be resource-intensive for the server, and tens of thousands or even billions of clients, such as Web sites, with short connections can save resources.

TCP/IP communication interpreting short and long links

One. Communication mode
There are three major categories:
A Server/client Way
1. A client connects to a server party, or peer to peer:
2. Multiple clients connect to one server side, which is also the usual way of concurrent servers.
3. It is rare for a client to connect to multiple server parties, mainly
Used by one customer to send requests to multiple servers.

(b) Connection method
1. Long connection
The client and the server first establish a communication connection, after the connection is established,
The message is then sent and received. This way, because the communication connection has been
exists, you can use the following command to see if the connection is established:
Netstat–f inet|grep port number (e.g. 5678).
This approach is often used for point-to-point communication.

2. Short connection
The client party communicates with the server every time the message is sent and received.
Immediately after the transaction is complete, disconnect the connection. This approach is often used in a point-to-multipoint
Communications, such as multiple client connections to a server.

(c) Sending and receiving mode

1. Asynchronous
Message sending and receiving are separate, independent and mutually exclusive. Such parties
There are two types of cases:
(1) Asynchronous duplex: Receive and send in the same program, there are two different
Child processes are responsible for sending and receiving each
(2) Asynchronous Simplex: Receive and send is done with two different programs.
2. Synchronization
The message is sent and received synchronously, which waits to receive the return message after the message is sent.
Synchronous mode generally need to consider the time-out problem, that is, the message can not be unlimited after sending
Time, the sender will no longer wait for the read return report
The direct notification timeout is returned.

The actual mode of communication is a combination of these three types of communication methods. For example, the general book provides
The TCP/IP paradigm programs are mostly server/client programs that synchronize short connections. Some
The combination is basically unused, the more commonly used valuable combinations are the following:

Synchronous Short Connection Server/client
Same Step connection Server/client
Asynchronous Short Connection Server/client
Asynchronous long-connected duplex server/client
Asynchronous long-connected single-work server/client

Where asynchronous long-connected duplex is the most complex form of communication, sometimes by
There is often a communication between two sets of systems between different banks or different cities.
such as Gold Card project. Since these modes of communication are relatively fixed, they can be pre-
First of all, these kinds of communication methods of the template program.

Two. Message format

Communication message format is more diverse, corresponding to the need to design a corresponding read-write message connection
Receive and send a delivery paper function.

(i) Blocking and non-blocking modes 
1. Non-blocking mode
Read function continuously read action, if no message received, wait for a period of time
Time-out returns, which typically requires a timeout to be specified.
2. Blocking mode
If no message is received, the read function waits until a message arrives.

(b) Circular reading and writing mode
1. A direct read and write message
In a single receive or send a newspaper text action in one time without adding the full read or all
Send the report text section.
2. Do not specify the length of the loop read/write
This generally occurs in the short connection process, limited by network routing, a longer
The text may be decomposed into several packages during the transmission of the network. One read may not
Can read all the messages once, which requires the circulation of newspapers, until the end of reading.
3. Loop reading and writing with length message head
This is generally the case in the long connection process, because there is no condition in the long connection to
Determine when the loop reads and writes are finished, so the length header must be added. Read function
First read the length of the message header, and then according to the length of the reading paper. In fact,
The code format of the header is also often different, if the message header is non-ASCII, you must also
Converted to ASCII, the common message header code system is:
(1) n bytes of ASCII code
(2) N-byte BCD code
(3) N-byte network integer code

Http long connections and short connections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.