The TCP connection starts with 3 way handshake .
At the time of 3way handshake, TCP payload does not contain any data. Upon completion of this 3-way handshake, data exchange of upper layer information (for example http) is started in TCP payload.
A host that starts a TCP connection (sending SYN first) is a client, and a host that receives a TCP connection (receives SYN first) is called a server.
Below is a sample TCP connection.
TCP data transmission
TCP is implemented with reliability of data with Seq#(sequence number) and Ack#(acknowledgment number) .
If data is missing, retransmission is performed when RTO(Retransmission TimeOut) is coming. RTO is calculated based on the RTT value.
The RTT value is the time from when data is sent until ack returns, and only one packet is sampled for each Window size.
In addition, this RTT value is used to calculate the Smooth RTT (SRTT) value. The calculation formula of SRTT value is as follows.
α is recommended 0.9.
The following formula is used for RTO.
End of TCP connection
A TCP connection is terminated in a half closed manner. In other words, we break the connection one by one.
If you want to disconnect the connection, set the FIN bit and give the TCP segment to the opponent.The side that first issues FIN is called Active Close .
On the other hand, the side that received the FIN sets the FIN bit together with the ACK bit and transmits the TCP segment.The side that later issues FIN is called Passive Close .
Although Active Close can be done from either side, in most cases, the client will Active Close.(It depends on how to use the socket API in the application)
There is also an implementation that "the side that received the FIN first sends a TCP segment with the ACK bit set and waits for a certain wait time before sending the TCP segment with the FIN bit set", but as mentioned above, ACK and FIN There are also implementations that make it together.This area depends on how it is implemented in the application.
For studying TCP / UDP, the following books are recommended.