JavaEE-Easy to understand the TCP protocol of network principles

TCP protocol

TCP, that is, Transmission Control Protocol, Transmission Control Protocol.

TCP protocol data format

insert image description here

  • The 16-bit source port number and the 16-bit destination port number indicate which process the data is coming from and which process to go to .
  • The 32-bit sequence number represents the number of each byte of the byte stream in a certain transmission direction during a TCP communication (from TCP connection establishment to disconnection) (TCP numbers each byte of data, called serial number).
  • 32 is the acknowledgment sequence number indicating the receiver's response to the sent tcp message segment. Its value is the serial number value of the received TCP segment plus 1 .
  • The 4-bit TCP packet length indicates how many 32-bit bits (how many 4 bytes) the TCP header has, so the maximum length of the TCP header is 15 * 4 = 60 bytes .

6 flags:
URG: Indicates whether the urgent pointer is valid
ACK: Whether the confirmation number is valid
PSH: Prompts the receiving end application to read the data from the TCP buffer immediately
RST: Indicates reset connection, that is, re-establishes the connection
SYN: Indicates synchronization, The one carrying the SYN mark is called the synchronous message
FIN: to inform the other party that the local end is going to be closed, and we call the one carrying the FIN mark the end message segment

  • 16-bit window size: indicates that the receiving end TCP can provide a maximum buffer of 65535 bytes.
  • 16-bit checksum: The sender fills in the results obtained by performing certain mathematical calculations with the TCP header and message data, and the receiver calculates with the same mathematical method. If the checksums are the same, the verification is successful.
  • 16-bit urgent pointer: identifies which part of the data is urgent data.

TCP principle

confirmation response

After receiving the sender's data, it will return to send an ACK message with the next byte sequence number of the last byte of the received data.

For example, if the serial number of the last byte received is 1000, the returned serial number is 1001, which means that all the first 1000 bytes of data have been received.

To solve the problem, tcp will have a receiving buffer (memory space in the kernel), each socket has its own receiving buffer, and tcp can queue up the received messages in the buffer according to the serial number.

timeout retransmission

Packet loss may occur in network transmission:

  1. The datagram sent was not received by the receiver
  2. After the recipient receives it, the ack message sent back is lost during transmission.

Response strategy
If the sender does not receive the ack response message, it will resend the message at intervals. As the number of retransmissions increases, the interval time will also increase. After multiple consecutive retransmissions, the ack message still cannot be obtained. tcp will try to reset the connection, if the reset fails, it will release the connection (TCP will dynamically calculate the maximum timeout period in order to ensure high-performance communication in any environment).

If retransmitted data is received and the data is duplicated, tcp will automatically deduplicate according to the serial number of the received data

How does tcp achieve reliability?

  答:确认应答+超时重传

connection management

three handshake

insert image description here
The above process is automatically completed by the kernel, and the application program cannot intervene. The purpose
of the handshake is to perform a network interaction between the two communicating parties, which is equivalent to the client and the server. Through three interactions, a connection is established (both parties record each other's information). The purpose of the three-way
handshake It is to ask for directions and verify whether the sending and receiving capabilities of the client and server are normal!

The client sends a SYN message to the server to ask the server if you are ready, and the server returns a SYN+ACK message saying that I am ready, how about you? The client sends an ACK to tell the server that I am ready, and the connection is established successfully.

waved four times

insert image description here
To disconnect, both the server and the client may first initiate a FIN end message.
Why is it waved four times instead of three times?
There is a certain probability that ack and fin will be merged into one!! But usually Cannot be merged!
ack and fin will not be triggered at the same time, because ack is completed by the system kernel, but fin is controlled by the application program, and the call to the close method of the socket will trigger fin!!!

sliding window

The sender sends multiple pieces of data at a time and stops sending to a certain extent. The sender waits for multiple acks at a time, receives an ack message and sends a piece of data, which looks like a sliding window.
insert image description here

insert image description here
Packet loss occurs:

  1. The data packet arrives and the ACK message is lost.
    This situation has no effect. As long as the last few ack messages are received, it means that the previous acks have also been received (the meaning of the confirmation sequence number means that all the data before the sequence number has been received) receive).
  2. packet did not arrive
    insert image description here

If this is the case, the response message always shows that 1001 has been received, 1001 to 2000 will be timed out and retransmitted, and the acceptance of subsequent data will not be affected. When 1001 to 2000 is received, it will directly return the accepted data The sequence number of the next byte of the last byte.
The above-mentioned timeout retransmission is called fast retransmission.

flow control

The essence is to allow the receiver to limit the speed of the sender - to determine the sending speed of the sender according to the processing capability of the receiver .
When the ack bit in the header of the ACK message is 1, the window size field of the header will take effect and return a The size of the window determines the sending speed of the sender.
When the sender finds that the receiving buffer of the other party is full, it will suspend sending, but it will still trigger a "window detection" message every once in a while. If the detection will find the window If the size is not 0, continue sending.

congestion control

TCP introduces a slow start mechanism, which sends a small amount of data first, explores the path, finds out the current network congestion state, and then decides how fast to transmit data. The processing capacity of the transmission path is measured - the barrel
effect (the transmission path is strong on a certain The available transmission rate of a router is very small, so the transmission capacity of this router is the maximum transmission capacity on the transmission path)
Through experiments, find a suitable transmission rate - start sending at a smaller rate first, without packet loss Increase the rate in the case, if the packet is lost, immediately reduce the rate to achieve a dynamic balance .

    实际发送的窗口大小=将拥塞窗口和接收端主机反馈的窗口大小(流量控制窗口)做比较,取较小的值作为

The actual sending window
insert image description here
is described in this figure: The congestion window is transmitted at a small value first, and the rate starts to increase exponentially when there is no packet loss, and grows linearly after reaching a certain threshold (avoid exceeding the upper limit by a lot at once). When packet loss occurs, it is considered that the current window has reached the upper limit of the path, and the window returns to a smaller value, and the threshold is updated to be half of the upper limit;

delayed response

The core of the delayed response is to delay the response of the ACK response message for a while, then the receiver's buffer size in the response message is likely to be larger than the immediate return (the application has been reading data from the receiving buffer), At this time, the sender can send a little more data.
The larger the window, the greater the network throughput and the higher the transmission efficiency. Try to improve the transmission efficiency while ensuring that the network is not congested.
Quantity limit: every N packets Respond once;
Time limit: Respond once when the maximum delay time is exceeded;

piggybacking

The receiver's response message ack waits a while for the receiver's response message to be combined into one datagram for transmission, which can improve efficiency. The
effect of piggybacking the response----four waved hands can be done, and three waves are finished.

stream-oriented

Sticky packet problem
A continuously sends multiple application layer datagrams to B, and these data will accumulate in the receiving buffer of B. At this time, it will be difficult for the application program of B to read these datagrams to distinguish which one is a complete one. It is easy to read half of the datagram ( in the TCP protocol header, there is no such field as the "message length" like UDP, from the perspective of the application layer, what you see is only a series of continuous byte data, I don't know which part to start from, it is a complete application layer packet ).
Solution:

  1. Convention separator.
  2. Agreed length (for example, the first four bytes of the agreed data are used to indicate the length of the datagram).

abnormal problem

  1. Process shutdown/process crash
    The process is gone, the socket is a file, and it is closed. Although the process is gone, the connection is still there, and you can wave your hands four times normally.
  2. The host shuts down
    . All user processes are shut down, triggering four waves of hand. If the wave is not finished, for example, the server sends fin, the client shuts down before the ack arrives. At this time, the peer will retransmit
    fin...after several retransmissions After finding that there is no ack, try to reset the connection, and release the connection if it fails.
  3. Power outage/network outage
    1. If the other party is the sender,
      the other party will not receive ack=>timeout retransmission->reset connection->release connection
    2. If the other party is the receiving end
      , the other party will not be able to know immediately whether your side has not had time to send the data or has simply disappeared.
      This makes the tcp heart-watching packet keep-alive mechanism play a role

The receiver periodically sends a heartbeat packet to the sender. If there is a response, it means that the peer is still alive. If there is no response, it means that the peer has hung up (the heartbeat packet is not so timely periodically, and the judgment is not so strict).

Guess you like

Origin blog.csdn.net/st200112266/article/details/130261046