TCP three-way handshake/four waved hands, TCP long connection and short connection

TCP three-way handshake/four waved hands

Three handshake

The first handshake: Host A sends a bit code of syn=1, and randomly generates a data packet with seq number=1234567 to server host B. Known by SYN=1, A requests to establish a connection;

The second handshake: Host B needs to confirm the online information after receiving the request, and sends ack number=(seq+1 of host A), syn=1, ack=1, randomly generating seq=7654321 packet;

The third handshake: host A checks whether the ack number is correct after receiving it, that is, the first sent seq number+1, and whether the bit code ack is 1, if it is correct, host A will send another ack number=(host B’s seq+1), ack=1, host B confirms after receiving it.
Insert picture description here

Wave four times

(1) A three-way handshake is required for TCP to establish a connection, and four times for disconnection.This is caused by the half-close of TCP

Because the TCP connection is full-duplex (that is, data can be transmitted in both directions at the same time), each direction must be closed separately when closing . This one-way closing is called semi-close.

When one party completes its data sending task, it sends a FIN to notify the other party that the connection in this direction will be terminated.

(2) Close the connection from the client to the server: First, client A sends a FIN to close the data transmission from the client to the server, and then waits for the server's confirmation. Among them, the termination flag FIN=1, the serial numberseq=u

(3) When the server receives this FIN, it sends back an ACK, and the acknowledgment number ACK is the received sequence number plus 1.

(4) Close the connection from the server to the client: it also sends a FIN to the client.

(5) After receiving the FIN, the client segment sends back an ACK message for confirmation, and sets the confirmation sequence number seq to the received sequence number plus 1. The party that shuts down first will execute the active shut down, and the other party will execute the passive shut down.
Insert picture description here
After host A sends the FIN, it enters the termination waiting state. After
server B receives the connection release message segment from host A, it immediately sends an acknowledgment to host A, and then server B enters the close-wait state.
At this time, the TCP server process informs the higher layer The application process, and thus the connection from A to B is released. At this time, it is in the "semi-closed" state.
That is, A cannot be sent to B, but B can be sent to A. At this time, if B has no datagram to send to A, its application process will notify TCP to release the connection, and then send to A connection release segment, and wait for confirmation.
After A sends the confirmation, it enters time-wait. Note that the TCP connection has not been released at this time, and then waits for the 2MSL set by the timer before A enters the close state.

Why is there TIME_WAIT status:

  • Ensure that there is enough time for the other party to receive the ACK packet
  • Avoid confusion between old and new connections

TCP/IP status

LISTENING(listening)

  • After the FTP service is started, it is first in the listening (LISTENING) state.

ESTABLISHED(established)

  • establish connection. Indicates that the two machines are communicating.

CLOSE_WAIT

  • If the other party actively closes the connection or the network is abnormal, the connection is interrupted. At this time, our state will become CLOSE_WAIT. At this time, we must call close() to make the connection close properly.

TIME_WAIT

  • Our party actively calls close() to disconnect, and the status changes to TIME_WAIT after receiving the other party's confirmation.

SYN_SENT

  • The SYN_SENT state indicates a connection request. When you want to access the services of other computers, you must first send a synchronization signal to the port. At this time, the state is SYN_SENT. If the connection is successful, it will become ESTABLISHED.

TCP long connection and short connection

Reasons for using long and short connections

When using network communications TCP协议when, before the actual reading and writing, serverand clientmust establish a connection between reading and writing when the operation is completed, the two sides no longer need this connection they can release this connection, the need to establish a connection is three-way handshake Yes, and release requires 4 waves of hands, so the establishment of each connection requires resource consumption and time consumption.

HTTP long and short connections

HTTP long and short connections are essentially TCP long and short connections

In HTTP/1.0, short connections are used by default.

In other words, each time the client and server perform an HTTP operation, a connection is established, and the connection is terminated when the task ends .

When 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.), the browser will re-establish each time such a Web resource is encountered An HTTP session.

Since HTTP/1.1, long connections are used by default to maintain connection characteristics

Using the HTTP protocol for persistent connections, this line of code will be added to the response header:Connection:keep-alive

In the case of long connections, when a page is opened to complete, between the client and the server for the transmission HTTP数据of TCP连接not closed, the client to access the server again, we will continue to use a connection has been established.

Keep-Alive will not keep the connection permanently. It has a keep time, which can be set in different server software (such as Apache). To achieve a long connection, both the client and the server need to support the long connection.

TCP—long connection

The so-called long connection,It means that multiple data packets can be sent continuously on a TCP connection. During the TCP connection maintenance 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(No RST packet and four waved hands).

Connection→data transmission→keep connection (heartbeat)→data transmission→keep connection (heartbeat)→……→close connection (a TCP connection channel with multiple read and write communication) ; this requires long connection to send regularly when there is no data communication Data packet (heartbeat) to maintain the connection state;

TCP keep-alive function, keep-alive function is mainly provided for server applications, server applications want to know whether the client host crashes, so that it can use resources on behalf of the client.

If the client has disappeared, leaving a semi-open connection on the server, and the server is waiting for data from the client, the server should wait for the client's data far away. The keep-alive function is trying to detect this semi-open connection on the server side. connection.

TCP-short connection

Short connection refers to the establishment of a TCP connection when there is data exchange between the two parties. After the data is sent, the TCP connection is disconnected.(It is relatively simple to manage, the existing connections are all useful connections, and no additional control means are required);

Connect → data transfer → close the connection;

Application scenarios

Long connection: It is mostly used for frequent operation (read and write), point-to-point communication, and the number of connections cannot be too many. Each TCP connection requires a three-way handshake, which takes time. If each operation is connected first, then the processing speed will be reduced a lot, so each operation will not be disconnected, and the data packet will be sent directly during the second processing. It's OK, and there is no need to establish a TCP connection.

  • For example, the database connection uses a long connection. If a short connection is used, frequent communication will cause socket errors, and frequent socket creation is a waste of resources.

Short connection: http services like WEB sites generally use short links (http1.0 only supports short connections, 1.1keep alive with time, long connections with limited operations), because long connections will consume a certain amount of resources for the server , And the frequent connection of thousands or even hundreds of millions of clients like the WEB website will save some resources by using short connections;

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. Therefore, the amount of concurrency is large, but each user needs to use short connections without frequent operations;

Guess you like

Origin blog.csdn.net/qq_32727095/article/details/114162028