Long and short connections, simplex, half and full duplex

The so-called long connection means that multiple data packets can be sent continuously on a TCP connection. During the TCP connection holding period, if no data packets are sent, both parties need to send detection packets to maintain the connection. Generally, you need to do online maintenance by yourself.
Short connection refers to When there is data exchange between the two communication parties, a TCP connection is established. After the data is sent, the TCP connection is disconnected. Generally, banks use short connections,
such as http, which only connect, request, and close, and the process time is short. The connection can be closed if no request is received within the time.
In fact, a long connection is relative to the usual short connection, that is, the connection state between the client and the server is maintained for a long time.

The operation process of long connection and short connection The
usual short connection operation steps are:
connection→data transmission→close connection;

and long connection is usually:

connection→data transmission→keep the connection (heartbeat)→data transmission→keep the connection (heartbeat)→ ……→Close the connection;
this requires the long connection to send data packets (heartbeat) regularly to maintain the connection state when there is no data communication, and the short connection can be directly closed when there is
no When to use a long connection, a 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,  frequent communication with short connections will cause socket errors, 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. Some resources, if you use a long connection, and there are thousands of users at the same time, if each user occupies a connection, it can be imagined. 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.
Sending and receiving mode

1. Asynchronous
Message sending and receiving are separate, independent of each other, and do not affect each other. This method is divided into two cases:
(1) Asynchronous duplex: receiving and sending are in the same program, and two different sub-processes are responsible for sending and receiving respectively
(2) Asynchronous simplex: receiving and sending are performed using two different procedures to complete.
2.
Synchronization The sending and receiving of the message are carried out synchronously, that is, after the message is sent, it waits for the return message to be received. The synchronization method generally needs to consider the timeout problem, that is, after the message is sent, it cannot wait indefinitely, and a timeout period needs to be set. After this time, the sender no longer waits for the read return message, and directly informs the timeout to return.

In a long connection, there is generally no condition to judge when the read and write ends, so the length header must be added. The read function first reads the length of the message header, and then reads the message of the corresponding length according to this length.

Simplex, half-duplex and full-duplex

According to the division of labor and the direction of signal transmission between the two communication parties, communication can be divided into three modes: simplex, half-duplex and full-duplex.
In the computer network, the duplex mode is mainly used, among which: the local area network adopts the half-duplex mode, and the metropolitan area network and the wide area network adopt the full biennial mode. 
1 Simplex (Simplex) mode: The transmitter and the receiver in the devices of both sides of the communication have a clear division of labor, and data can only be transmitted in a single fixed direction from the transmitter to the receiver. A typical sending device using simplex communication is a card reader of an early computer, and a typical receiving device is a printer.
2. Half Duplex mode: The devices on both sides of the communication are both transmitters and receivers. The two devices can transmit data to each other, but they can only transmit data in one direction at a certain time. For example, walkie-talkies are half-duplex devices because only one party can speak at a time.
3. Full Duplex: The devices on both sides of the communication are both transmitters and receivers, and the two devices can transmit data in both directions at the same time. For example, a telephone is a full-duplex device because both parties can talk at the same time.


Guess you like

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