Long connections and short connections
The so-called long connection, refers to a TCP connection can be continuously sent multiple packets, during the TCP connection remains, if no packet sent, need to send a detection packet to maintain this connection, usually need to do their own online maintenance.
Short connection refers to the communication between the two parties have data interaction, the establishment of a TCP connection, after the data is sent, the TCP connection is disconnected, the general banks use short connections.
For example, HTTP, just connect, request, close, the process time is short, the server can not receive a request for a period of time to close the connection.
In fact, long connections are relative to the usual short connection, that is, long time to maintain the client-server connection state
Long connection and short connection operation process
The usual short connection operation steps are:
Connection → data transmission → close connection;
A long connection is usually:
Connect → data transfer → keep connected (heartbeat) → data transfer → keep connected (heartbeat) → ... → Close connection;
This requires a long connection in the absence of data communication, the timing of sending a packet (heartbeat) to maintain the connection state, short connection in the absence of data transmission directly off the line
When to use long connections, short connections?
Long connections are used to operate frequently, point-to-point communications, and the number of connections can not be too many cases. Each TCP connection requires a three-step handshake, which takes time, if each operation is first connected, then operation then the processing speed will be reduced a lot, so after each operation is continuously open, the processing of the direct sending of packets is OK, do not establish a TCP connection. For example, the connection of the database with a long connection, if the frequent communication with short connections will cause socket errors, and frequent socket creation is also a waste of resources.
Send Receive Mode
1. Asynchronous
Message sending and receiving are separate, independent and mutually exclusive. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, by two different sub-processes are responsible for sending and receiving respectively
(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 wait indefinitely after sending out, need to set the timeout period, more than the time the sender no longer wait for the read return message, the direct notification timeout return.
In a long connection, it is generally not possible to determine when the read-write ends, so it is necessary to add a length message header. The reading function reads the length of the message header, and then reads the corresponding length of the message according to the length.
Simplex, half-duplex, and full-duplex
The communication can be divided into three modes according to the Division of labor and the direction of signal transmission: simplex, half duplex and full duplex. In the computer network, the main use of duplex, wherein: LAN using half-duplex, metropolitan area Network and WAN adopt the whole two-year approach.
1. Simplex (Simplex) mode: The transmitter and receiver in the communication devices are clearly divided, only in the transmitter to the receiver of a single fixed direction of transmission of data. A typical transmitting device using a single-work communication, such as a reader of an earlier computer, typically a receiving device such as a printer.
2. Half-duplex (half Duplex) mode: Communication both devices are both transmitters and receivers, two devices can transmit data to each other, but a moment can only transmit data in one direction. For example, a walkie-talkie is a half-duplex device, because only one party can speak at a time.
3. Full Duplex mode: Both devices are both transmitters and receivers, and both devices can transmit data in two directions at the same time. For example, the phone is a full-duplex device because both sides can speak at the same time.
HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, and tens of thousands or even billions of clients, such as Web sites, use short connections to save resources if they are connected with long connections, and thousands of users at the same time, If each user occupies a connection, then you can imagine it. So the concurrency is large, but each user needs to be short-connected in case of frequent operation.
In summary, the choice of long connections and short connections is subject to availability.
Long connections and short connections for sockets