The TCP Protocol of Network Protocol

Source: Internet
Author: User

Overview

Both TCP and UDP use the same network-layer IP, but unlike UDP, TCP is a connection-oriented, reliable byte-stream protocol. Therefore, both parties must establish a TCP connection before transmitting the data. TCP realizes the transmission of reliability through the mechanism of verification, serial number, acknowledgment response, retransmission mechanism, Connection management and window control.

TCP provides reliability in the following ways:

    1. The application data is divided into TCP segments that TCP considers the most appropriate data block to send. This is quite different from UDP, where the length of UDP datagrams produced by the application will remain the same, and the IP layer will be IP-partitioned when the UDP datagram is added to the IP header, if necessary.
    2. When TCP sends a message segment, it initiates a timer, waits for the destination to acknowledge receipt of the message segment, and if the acknowledgement is not received in time, the segment will be re-sent. That is, time-out retransmission.
    3. When the TCP receiver receives a TCP segment from the other end of the connection, it sends a confirmation that the acknowledgment is not sent immediately and is typically deferred for a fraction of a second. That is, confirm the answer.
    4. TCP will maintain the checksum of its header and data. This is an end-to-end checksum to check for any changes in the data during transmission. If there is a error in the checksum of the received segment, TCP discards the segment and does not send acknowledgement of the received message, which causes the sender to time out to resend.
    5. Because the TCP message segment is transmitted as an IP datagram, the arrival of the IP datagram may be out of order, so the arrival of the TCP message segment may be out of order, and if necessary, TCP will reorder the received data and hand the received data to the application layer in the correct order.
    6. Because of the possible duplication of IP datagrams, the receiving side of TCP must discard duplicate data.
    7. TCP can provide flow control. Each side of the TCP connection has a fixed-size buffer space, and the receiving side of TCP only allows the other end to send the data that the receive-side buffers can accept, which prevents a faster host from causing a slower host buffer overflow. That is, flow control.

TCP Header

The TCP header looks like this:


The TCP header is much more complex than the UDP header, and there is no field that represents the packet length and data length, which can be obtained by the IP layer for TCP packet length, and the length of the data by the TCP packet length. The following is a brief description of the TCP header fields:

  • Source Port number : Indicates the sender port number, the word length 16 bits;
  • Target Port number : Indicates the receiver port number, the word length 16 bits;
  • Serial number : The length of the word 32 bits, refers to the location of the data sent, that is, from the sending side to the receiving end of the stream of bytes sent. Each time the data is sent, the size of the data byte count is incremented once. The sequence number does not start at 0 or 1, but rather the random number generated by the computer when the connection is established as its initial value, which is passed to the receiving host by the SYN packet. The number of bytes per forwarded past is then added to the initial value representing the location of the data. In addition, the SYN packets and FIN packets that are sent when the connection is established and disconnected do not carry data, but also add the corresponding sequence number as a byte. A TCP connection is established to initialize the serial number;
  • Confirm Answer : The word length 32 bits, refers to the next time you should receive the serial number of the data. In fact, it refers to data that has received a confirmation response number minus one. When the sender receives a confirmation answer, it can be considered that the data before the serial number has been received normally;
  • header Length (data offset): This field indicates which portion of the data transmitted by TCP should be computed from which location of the TCP packet, and can be viewed as the length of the TCP header. The word has a length of 4 bits, which is 4 bytes (32 bits). If the option field is not included, the TCP header length is 20 bytes, so the data offset field can be set to 5. Conversely, if the field value is 5, then the TCP packet from the beginning to 20 bytes is the TCP header, the remainder is TCP data;
  • reserved : This field is mainly used for later expansion, its length is 4 bits, generally set to 0, but even if the received package is not 0 in the field, the package will not be discarded;
  • Control bit : The word length is 8 bits, each from left to right respectively is CWR, ECE, URG, ACK, PSH, RST, SYN, FIN. These control flags are also called control bits. When their corresponding bit value is 1 o'clock, the specific meaning is as follows:
    • CWR : The CWR flag and the following ECE flag are used for the IP header of the ECN field, the ECE flag is 1 o'clock, then notify the other party has narrowed the congestion window;
    • ECE : If its value is 1, it notifies the other party that the network is blocked from the other side. In the IP header that receives the packet, the ECN is set to 1 in the TCP header at 1 o'clock;
    • URG : This bit is set to 1, which indicates that there is data in the package that needs urgent processing, and for the data that needs to be processed urgently, the following emergency pointers
    • ACK : This bit is set to 1, confirming that the answer field is valid, TCP specifies that the bit must be set to 1 in addition to the SYN packet when the connection was initially established;
    • PSH : This bit is set to 1, indicates the need to send the received data immediately to the upper application protocol, if set to 0, the data is cached first;
    • RST : This bit is set to 1, which indicates that an exception to the TCP connection must be forced to disconnect;
    • syn : Used to establish a connection, the bit is set to 1, indicating that you want to establish a connection, and in its serial number of the field to set the initial value setting;
    • FIN : This bit is set to 1, indicating that no data will be sent in the future and you want to disconnect. The TCP segment with FIN position 1 can be exchanged between the hosts on both sides of the communication when the communication ends when the connection is expected to be disconnected. Each host can be disconnected after confirming the other's FIN package. However, the host receives a TCP segment with Fin set to 1 and does not have to reply to a fin package immediately, but can wait until all the data in the buffer is automatically deleted after it has been successfully sent, and then the fin packet is sent;
  • window size : The length of the word is 16 bits, which is used to inform the size of the data (8 bytes) that can be received from the point where the acknowledgment answer number of the same TCP header begins. TCP does not allow the sending of data that is larger than the window size. A window of 0 indicates that a window probe can be sent to see the latest window size, but this data must be 1 bytes;
  • Test and : The TCP test and the same as the UDP test, but also the use of pseudo-header, but the detection of TCP and can not be closed. TCP pseudo-header information is the same as UDP, including: Source IP address, destination IP address, padding, protocol number, and TCP packet length;
  • Emergency pointer : This field is 16 bits. Valid only if the URG control bit is 1 o'clock. The numeric value of this field represents the pointer to the emergency data in this section. Emergency data is from the first part of the data section to where the emergency pointer is located. Therefore, the emergency pointer indicates the position of the end of the emergency data in the message section;

Serial number and confirmation answer

In TCP, when the data on the sending side reaches the receiving host, the receiving host returns a message notification that the acknowledgment is answered (ACK). TCP to confirm the answer and serial number to achieve reliable data transmission, if the sender sends the data after the receiving end of the acknowledgement response, it indicates that the data has successfully reached the receiving end, otherwise the data may be lost.


Time-out retransmission

Time-out retransmission refers to the specific interval of time that waits for a confirmation answer before the data is re-sent. If the sender sends out the data and does not receive an acknowledgement from the receiving end at a certain time, the sending side will resend the data, which is the timeout retransmission mechanism.

Connection Management

The TCP protocol is to provide connection-oriented communication transmission, which means that both parties must establish a connection before transmitting data. The connection and release connections for TCP are explained in the following sections, where only TCP presence knowledge is mentioned.

Window control

TCP transmits data in 1 segments, each sending a segment to confirm the processing of a reply, so that the packet round-trip time for communication is very long resulting in reduced communication performance. To solve this problem, TCP introduces window control, confirming that the answer is no longer in each segment, but in a larger unit to confirm, so that the forwarding time is shortened, that is, the sending side of the host after sending a segment does not have to wait for a confirmation answer, but continue to send data segments. Window size refers to the maximum number of data that can be sent without waiting for a confirmation answer.




The use of window control mechanism must implement the buffer, in Figure 4, the data in the window can be sent even if the acknowledgement is not received. In addition, the data from this window can be seen because some of its data has been lost in the transmission, so the sender can receive a confirmation reply, in which case it is necessary to re-send. To do this, the sending host must keep this part of the data in the buffer until the acknowledgment answer is returned. Other than the sliding window, the data that has not been sent has already been confirmed to the end of the data already received. The data can be removed from the buffer when the data is sent and the acknowledgement is received as expected, without re-sending. After you receive the acknowledgement answer, slide the window to the location of the serial number in the confirmation answer, which allows multiple segments to be sent simultaneously to provide communication performance. This mechanism is also known as the sliding window mechanism.

Flow control

Flow control allows the sending side to control the amount of data sent based on the actual acceptance of the receiving end. The specific operation is that the receiving host notifies the sending host that it can receive the size of the data, and the sender sends no more data than that size, which is the window size, that is, the window size is determined by the receiving side host. In the TCP header, there is a single field to notify the window size, and the receiving host places the buffer size that it can receive in the field to notify the sending side. When the receiving side of the buffer face data overflow, the window size of the value is also changed, set to a smaller value to notify the sender, so as to control the amount of data sent, so as to achieve the control of traffic.

Congestion control

To prevent network congestion, TCP uses a slow-start algorithm to control the amount of data sent. In order to adjust the amount of data sent, the congestion window is introduced, the Congestion window is set to 1 data segments to send data at slow start, then the value of the congestion window is added 1 after each acknowledgement reply. When sending a packet, compare the size of the congested window with the window size of the receiving host notification and select a smaller value to control the amount of data sent.


Resources:

"TCP/IP detailed"

"Graphic TCP/IP"

The TCP Protocol of Network Protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.