1.TCP Head Structure
As shown in the TCP header structure, many of these fields provide sufficient information to manage the TCP connection and control the data flow.
16-bit port number: tells the host where the message segment is from and to which upper layer protocol or application (destination port).
32-bit ordinal: the number of each byte of a stream in a transmission direction during a TCP communication. Assuming that host A and Host B do TCP traffic, a sends the first TCP segment of A to B, and the ordinal value is initialized by the system to a random value of isn. Then in the direction of the transmission (from A to B), the ordinal value in the subsequent TCP segment will be set to the system and the first byte of the data carried by the segment is offset in the whole byte stream.
32-digit Confirmation number: Used as the corresponding TCP message segment sent to the other party. The value is the received TCP message segment value plus one.
4-bit head length: Identifies how many 32bit of the TCP header.
16-bit window size: is a means of TCP traffic control. Tell the other side how many bytes of data the TCP accept buffer can hold, so that the other side can control the speed of sending the data.
2.TCP Connection setup and shutdown
The message to grab the TCP three handshake and four waves with Tcpdump is as follows:
This is illustrated below:
The TCP connection is full-duplex, so it allows data transmission in two directions to be shut down independently. That is, the communication party can send an end message segment to the other side, tell it that the end has completed the data sent, but allow to continue to accept data from each other, until the other side also send an end message to close the connection, TCP said this state is semi-closed state, as shown in:
The socket network programming interface shutdown function provides support for half-off. When the close () function is called, both the read and write are closed at the same time, and both ends close the socket connection. When you call shutdown, you can specify that only write is turned off, at which point the semi-closed state is entered.
3.TCP State Transitions
A party that actively shuts down a TCP connection does not directly enter the closed state after receiving the end segment of the other party, but instead moves to the TIME_WAIT state. This state, which generally lasts 2MSL (Maximum Segment life, the maximum lifetime of the message segment), can be completely closed. This is done primarily to ensure that the other party receives an ACK to the end message segment to ensure that the network can be reliably terminated. We can force the process to immediately use the port occupied by a connection in the TIME_WAIT state through the socket option SO_REUSEADDR.
4. Reset Message Segment
There are three situations where one end of a TCP connection sends a segment of the message carrying the RST flag to the other end.
(1) Access to a port that does not exist
When the client program accesses a nonexistent port, the target host sends it a reset message segment.
(2) Abnormal termination of connection
TCP provides an unusual way to terminate a connection by sending a reset message segment to the other. Once the reset message segment is sent, all data queued to be sent by the sending side is discarded.
(3) Handling semi-open connections
One end of TCP closes or abnormally terminates the connection, and the other side does not receive an end message, at which point the other end maintains the original link. We call the state at this time semi-open. If the data is written to a semi-open connection at this point, the other party responds with a reset message segment.
5.TCP Time-out retransmission
TCP maintains a retransmission timer for each TCP message, which starts when the TCP message segment is first sent. If the response is not received within the timeout period, the TCP module will retransmit the TCP segment and reset the timer. Typically times out, the time of the TCP timer is doubled. The maximum number of passes is given by the parameters of the kernel.
"Linux High Performance Server Programming" Reading notes of the TCP protocol detailed