Table of contents
1. Establish a connection (three-way handshake)
3. The life cycle of a TCP connection
1. Flow control
Hosts on each side of a TCP connection set receive buffers for that connection.
Receive buffer of TCP receiver:
1. Reasons for providing flow control service
The application process will read data from the buffer, but not immediately when the data arrives. In fact, the receiving application may be busy with other tasks, and it may take a long time to read the data. If an application is reading data relatively slowly, and the sender sends too much, too quickly, the data sent can easily overflow the receive buffer of the connection.
2. Flow control service
is a speed-matching service, where the sender's send rate matches the receiver application's read rate.
3. Realization of flow control
TCP has the sender maintain a variable called the receive window to provide flow control.
- The value of the receiving window is told by the receiver to the sender, which is equal to the remaining space of the receiving buffer
- The sender limits the amount of unacknowledged data to no more than the receive window according to the value of the receive window
TCP is a full-duplex communication, so the senders at both ends of the connection each maintain a receiving window
The receiver puts the size of the remaining space in the receive buffer into the receive window field of the message segment it sends to the sender.
2. Connection management
1. Establish a connection (three-way handshake)
TCP establishes a connection between sender and receiver before exchanging segments
Step1: The client's TCP first sends a SYN segment to the server's TCP:
- Does not contain application layer data
- Flag bit SYN = 1, ACK = 0
- The customer randomly selects an initial serial number and puts it in the serial number field
Step2: The server receives the SYN message segment, allocates TCP cache and variables for the TCP connection, and replies to the client TCP with a SYNACK message segment:
- Does not contain application layer data
- Flag bit SYN = 1, ACK = 1
- Confirmation number: customer's initial serial number + 1
- The server randomly selects an initial sequence number and puts it in the sequence number field
Step3: The client receives the SYNACK message segment, allocates TCP cache and variables for the TCP connection, and replies to the server TCP with an ACK message segment:
- May contain application layer data
- Flag bit SYN = 0, ACK = 1
- Confirmation number: server initial serial number + 1
In each segment after the connection is established, SYN will be set to 0
2. Close the connection
After the client application process closes the socket:
Step1: The client sends a FIN segment to the server, where FIN = 1;
Step2: The server receives the FIN segment, replies with the ACK segment, and enters the semi-closed connection state;
Step3: The server sends a FIN segment to the client, and the client receives the FIN segment and replies with an ACK segment;
TIME_WAIT
Assuming ACK is lost, TIME_WAIT state causes client to retransmit
Step4: The server receives the ACK segment, and the TCP connection is closed.