TCP protocol principle: TCP Each send a message segment, the start of a timer, if the timer after the timeout has not received an ACK confirmation, the retransmission of the message. , the packet is sent by the buffer of a to b,b after the packet is received, an ACK acknowledgement packet is sent back to a, and a is freed from the buffer. Therefore, the packet is cached in a buffer until an ACK is confirmed.
In the TCP/IP protocol, the TCP protocol provides a reliable connection-oriented service, a three-time handshake (setting up a connection) and a four-time wave (closing the connection);sliding window mechanismCarry out flow control;
detailed illustration of the three-time handshake
The so-called three-time handshake (three-way handshake) means that when a TCP connection is established, the client and server are required to send a total of 3 packets. As shown in 1.
Figure 13 Handshake establish each state of a TCP connection
(1) First handshake: When the connection is established, client A sends the SYN packet [syn=1,seq=x] to Server B and enters the syn_send state, waiting for Server B to confirm.
(2) Second handshake: Server B receives the SYN packet, must confirm the SYN of customer A, and also sends a SYN packet itself, that is, Syn+ack packet [syn=1,ack=1,seq=y,ack=x+1], when Server B enters the SYN_RECV state.
(3) Third handshake: Client A receives Server B's Syn+ack package, sends confirmation packet to Server B ack[ack=1,seq=x+1,ack=y+1], this packet is sent, client A and Server B enter established state, complete three handshake. Three handshake is completed and the client and server begin to transfer data.
three times after the handshake is complete, the client and the server establish a TCP connection. Then you cancall the Accept function to get this connection。three-time handshake purposeis to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. In socket programming,when the client executes connect (), it will trigger a three-time handshake。
detailed illustration of four waves
The removal of TCP connections requires sending four packets, so called four waves (Four-way handshake). Either the client or the server can initiate a wave gesture, and in socket programming, any party performs a close () operation to generate a wave. Because TCP connections are full-duplex, each direction must be closed separately. The principle is that when a party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means there is no data flow in this direction, and a TCP connection can still send data after receiving a fin. The first party to close will perform an active shutdown, while the other side performs a passive shutdown. TCP to ensure that all possible circumstances so that all the data can be delivered, when you close a socket, the active closed one end of the socket will enter the TIME_WAIT state, and the passive shutdown side into the closed state, which ensures that all data is transmitted.
Figure 24 waving off each state of a TCP connection
(1) First the TCP process at the end of a B is in the established state, when a's application sends out the message segment, it will actively close the connection. A will stop sending the message segment (but will also receive) and send [FIN = 1,seq=u] data to B and then enter the FIN-WAIT-1 state;
(2) b after receiving a request sent, the application process is notified, A has no longer send data, and B will send ACK acknowledgement data to a [ack=1,seq=v,ack=u+1],b into the close-wait state, a received the data sent by B, A enters the fin-wait-2 state, where A to B connection is closed (that is, the semi-connected state).
(3) When the application process of B finds that there is no data to be transmitted, the B application process sends a passive shutdown request, B sends the [FIN=1,ACK=1,SEQ=W,ACK=U+1] data to a, and enters the Last-ack state;
(4) A after receiving the data sent by B, send ACK acknowledgement data to B [ACK =1,seq=u+1,ack=w+1], enter the time-wait state, wait for 2MSL after the normal shutdown connection into the closed state; B receives the acknowledgement sent by a to enter the closed state. B to a-side connection off! At this point, the TCP connection is really all shut down!
several questions:
1. Why is a connection agreement a three-time handshake, and a four-time handshake when the connection is closed?
This is because the socket in the listen state of the server receives a connection request from the client's SYN message, and it can send the ACK and SYN (ACK response, and SYN synchronously) in a message. However, when the connection is closed, when receiving the other's fin message notification, it simply means that no data is sent to you, but not all of your data are sent to the other side, so you may not immediately close the socket, that is, you may also need to send some data to the other side, Send the FIN message to the other side to show that you agree that you can now close the connection, so it is here that the ACK message and fin messages are sent separately in most cases.
2. Why does the time_wait state need to wait 2MSL before returning to the closed state?
This is because although both sides agree to close the connection, and the handshake of the 4 messages are also coordinated and sent, it can be directly back to the closed state (like from the Syn_send state to establish state); but because we have to assume that the network is unreliable, You cannot guarantee that the last ACK message you send will be received by the other party, so the socket in the Last_ack state may be re-sending the fin message because the timeout does not receive an ACK message, so this time_wait state is used to resend the possible missing ACK message.
3.TCP Message Segment
The TCP message segment is also divided into the header and the data two parts, the first default is generally 20 byte length, but in some cases, the use of "optional field", the header length will increase.
TCP header message information, there is a status control code (Code,control flag), also known as the flag bit field (U, A, P, R, S, F): 6 bits. The meanings of each bit are as follows:
URG: Emergency bit (urgent). When Urg=1, indicates that the emergency pointer field is valid and represents the packet as an emergency packet. It tells the system that there is emergency data in this segment and should be delivered as soon as possible (equivalent to high-priority data), and that the Urgent Pointer field in it will be enabled.
ACK: Confirm bit (acknowledge). The confirmation Number field is valid only when Ack=1, which represents the packet as a confirmation packet. The confirmation number is invalid when ack=0.
PSH: (Push function). If it is 1 o'clock, the representative asks the other party to send the other corresponding packets in the buffer immediately, without waiting for the buffer to be full.
RST: Resets the bit (reset). When rst=1 indicates a serious error in the TCP connection (for example, due to host crashes or other reasons), the connection must be released before re-establishing the transport connection.
SYN: Synchronous bit (synchronous). The SYN is set to 1, which means that this is a connection request or a connection acceptance message, and usually a packet with a SYN flag means "active" to connect to the other party.
FIN: Terminating bit (Final). Used to release a connection. When Fin=1, it indicates that the data of the sending end of this segment has been sent and the transport connection is released.
* The calculation of the confirmation number in the header format of the transmission data process *TCP message:
The confirmation number is the ordinal of the first data byte expected to receive the next message segment of the other.
The serial number equals the sum of the number of data bytes in the previous message segment.
4. TCP connections that have successfully established a connection can cause TCP to generate a critical error in the following four scenarios, sending the RST package :
1. Port not open
2. Request Timeout
3. Early closure
4. Receive data on a closed socket
Network Foundation---TCP connections