TCP three-way handshake process four times and waved

Three-way handshake

In order to establish a reliable TCP connection, the initialization process will handshake Seq initial values, thus ensuring the order of the communication, network problems prevent disorder;

TCP three-way handshake

The first handshake : connection is established, A sends a SYN packet (seq = x) to B, and enters SYN_SEND state, waiting for acknowledgment B;

Second handshake : B receiving the SYN packet, it must return an ACK packet to A (ack = x + 1) to confirm, at the same time he is sending a SYN packet (syn = y), i.e., SYN + ACK packet, and enters a state SYN_RECV ;

Third handshake : A B receives a SYN + ACK packet, the ACK packet sent to B (ack = y + 1) confirmed that this packet transmission is completed, A and B become established state;

 

Four waving

In order to terminate the TCP connection.

Four waving

The first wave: A sends a FIN (seq = x) packet, ACK (ack = y) {packet does not indicate the figure above] to disable data transfer A to B, A enters state FIN_WAIT_1;

Second wave: after receipt of the FIN B, sends an ACK (ack = x + 1) packet to A, and enters CLOSE_WAIT state, notify the application, the other required to close the connection A;

Third Wave: In this case the application B can notify TCP connection is closed, will send a FIN (seq = y, ack = x + 1) packets, data transfer B to disable the A, B enter state LAST_ACK;

The fourth wave: after receipt of the FIN A, A enters state TIME_WAIN, then returns an ACK (ack = y + 1 bag) to B, B into the CLOSED state, four complete wave;

Why TIME_WAIT state 2MSL time to wait?

1) to ensure there is enough time and they'll get an ACK packet;

        If the fourth wave of the ACK packet segment is lost, thus making in the LAST-ACK state B can not receive the final ACK packet sent by the other party. This timeout retransmission B FIN + ACK segment, and A can receive the retransmitted FIN + ACK segment within 2MSL time. However, if A does not wait for a period of time TIME-WAIT state, but sending the ACK segment immediately after release of the connection, can not receive FIN + ACK packet retransmitted segment B, and thus will not be transmitted again confirm segment. Thus, B can not follow the normal steps into the CLOSED state.

2) the old and new connections to avoid confusion;             

        A after sending ACK segment, and then after 2MSL time, can make the connection duration of the generated all segments have disappeared from the network. So you can make a new connection next segment of this old connection request does not occur.

 

Notes TCP packet header.

TCP header structure

Logo Description:

(1) source port number (6) and port number (16 bits): the port number of the communicating parties, are used to identify the application process.

(2) Serial number (32 bits), an acknowledgment sequence number (32): A packet processed in order to ensure the reliability of the connection;

(3) the length of the header / data offset: a maximum of 1111. Because the TCP header of a maximum of 60 bytes, but the [Option] indefinite length, how to locate the [Data] starting position by positioning the identifier.

(4) 6 Reserved: defining a new use for future reservations, now generally set to zero.

(5) control bit 6: 0 or 1:

        URG: urgent pointer flag is 1 indicates the urgent pointer is valid, 0 is ignored urgent pointer.

        ACK: acknowledgment number flag indicates a confirmation number is valid, 0 represents a free packet acknowledgment information, the acknowledgment number field is ignored.

        PSH: push flag of 1 indicates that data with the push flag indicating the recipient Upon receipt of the message segment, this segment should be handed over as soon as the application, rather than queuing in the buffer.

        RST: Reset the connection flags for resetting due to the host crash or some other reason error connection. Or to reject illegal segments and rejects the connection request.

        SYN: synchronous serial number for establishing a connection process, in a connection request, SYN = 1 and ACK = 0 indicates that the data segment is not used piggybacked acknowledgment field, connected to a piggybacked acknowledgment response, i.e., SYN = 1 and ACK = 1.

        FIN: finish flag, for releasing the connection, 1 means the sender has no data transmitted, i.e., close the side stream.

(6) 16-bit window: sliding window size, used to inform the sending end terminal receiving buffer size, thereby controlling the transmission rate of the data transmission side, so as to achieve the flow control.

(7) 16-bit checksum: parity function, the test range comprising a two part header and data, to check if there is erroneous transmission process, calculated by the sender and storage, verification by the receiving end.

(8) Options / filling: length of the common option is represented by the local MSS can accept the maximum segment, this field indicates the first handshake. To ensure option length is an integer multiple of 32, the filling will add extra zero bits.

  • Data: Data section of the TCP segment is optional. When a connection is established and a connection is terminated, the two sides exchanged only TCP segment header. If a party has no data to send, also did not use the header data to confirm the reception of any data. In many cases, the timeout process also transmitted without any data segment.
Released nine original articles · won praise 2 · Views 4078

Guess you like

Origin blog.csdn.net/qq_27085429/article/details/104765440