TCP protocol detailed (theory)

Source: Internet
Author: User
Tags ack


  • TCP protocol detailed (theory)

    1. Unlike UDP, TCP provides a connection-oriented, reliable byte-stream service. Connection-oriented is a good understanding, that is, the connection between the two before the communication needs to establish a pre-connection, this is like real life calls. To help the reliability, TCP protocol involves a number of rules to ensure the reliability of the communication link, summed up, the main points are as follows:

    (1) The application data is segmented into the data block that TCP considers most suitable for sending. This is partly controlled by the "MSS" (maximum packet length) option, which is often referred to as a negotiation mechanism, which specifies the length of the largest chunk of data that TCP travels to the other end. It should be noted that MSS can only appear in the SYN message segment, if one party does not receive the MSS value from the other side, the MSS will be set to 536 bytes. Generally speaking, in the case of no fragmentation, the MSS value is larger the better, this can improve the utilization of the network.

    (2) retransmission mechanism. Set the timer and wait for the confirmation package.

    (3) Check the header and data.

    (4) TCP sorts the received data and then gives it to the application layer.

    (5) The receiving end of TCP discards duplicate data.

    (6) TCP also provides flow control. (provided by the window size declared at each end)

    2. Header of TCP packet: www.2cto.com



    (1) If the option field is not counted, TCP's header accounts for 20 bytes.

    (2) The source port number and the destination port number are used to find the process of the originating and receiving end, generally speaking, through the port number and IP address, can uniquely determine a TCP connection, in network programming, is often referred to as a socket interface.

    (3) The serial number is used to identify the data byte stream sent from the TCP originator to the TCP receiver.

    (4) Confirm that the serial number contains the next sequence number expected to be received at the end of the sending confirmation, so the confirmation sequence number should be the last time the data byte sequence number plus 1 has been successfully received.

    (5) The header length indicates the length of the TCP header, and if no option exists, the value is 20 bytes.

    (6) Flag position (flag):

    URG: Emergency pointer valid

    ACK: Confirm serial number is valid

    PSH: The receiving party should give the segment to the application layer as soon as possible

    RST: Rebuilding the connection

    SYN: The synchronous sequence number is used to initiate a connection

    FIN: The originator completes the Send task (active shutdown)

    Explain

    TCP provides a workaround to let one end tell the other side that some "emergency data" has been placed in the normal data stream, allowing the receiver to do special processing of emergency data. At this point, the Urg bit is set to 1, and the 16-bit emergency data is set to a positive offset, which is added to the ordinal field in the TCP header, the last byte ordinal of the emergency data can be obtained, and the common application has the transmission interrupt key (during the connection through Telnet).

    RST: The Reset field is used when a message is sent to a socket interface and an error occurs, and TCP sends a reset message segment. The following are common occurrences: www.2cto.com

    Connection request sent to a nonexistent port: At this point the destination port is not listening, and for UDP, an ICMP unreachable error message will be issued, and for TCP, a datagram setting the RST reset flag bit will be issued. Abort a connection abnormally: Normally, by sending fin to gracefully close a TCP connection, it is possible to release a connection halfway through sending a reset message segment. In SOCKETAPI by setting the socket option So_linger to turn off this abnormal shutdown situation.

    3. TCP connection and termination process:



    (1) Three-time handshake:

    To establish a TCP connection, you must undergo three handshake processes where one end of the first SYN is sent to open actively, receiving this SYN and performing a passive open on the other end of the next syn.

    (2) Four releases:

    To release a TCP connection, you need to pass the four handshake process, which is caused by the semi-shutdown feature of TCP, because TCP connections are full-duplex, and therefore require that both ends of TCP be closed separately. It is worth noting that the active shut-off side after the fin is sent, still can normally receive the other party's data, just notify the other side it has no data to send, in the same way, the passive closed one end after receiving fin, still can send data, until it itself issued fin, before the data stop sending.

    (3) Timeout problem for TCP connection:

    The completion of a TCP connection involves a time-out problem, and most Berkeley systems have a time-out time limit of 75s,solaris9 of 240s, which is generally considered to be between 75-240.

    "Extended" in the specific implementation, how to complete the user to set the socket connection timeout time?

    "Workaround" The current implementation of the socket timeout connection is mainly through select to complete. The steps are as follows:

    Creating sockets

    Set the socket to non-blocking mode (if blocking mode, then the time setting is meaningless)

    Call Connect to connect

    Use Select to check if the socket is writable and to determine its results at the same time (why is it writable?). Because you need to detect if the socket receives an ACK. )

    Turn the socket into blocking mode

    (4) TCP semi-shutdown www.2cto.com

    The so-called "semi-close" refers to the ability of one end of a connection to receive data from the other side after it has been sent. Specifically, when the two sides of the handshake are completed three times, one side emits fin, at which point it will enter half-off

    Closed state, when it closes its own send function, but it can still receive the other side of the data, such as the other side sent an ACK message. So how is it implemented in real development?

    This involves the difference between the shutdown and close functions in the system.

    int shutdown (int s, int how) <sys/socket.h>

    The shutdown is used to terminate the socket interface specified by the parameter S, which is mainly in the following situations:

    how = 0 terminating a read operation

    how = 1 terminating a write operation

    how = 2 terminating read and write operations

    The returned errorcode may have:

    EBADF/* Bad file descriptor */

    Enotsock/* Socket operation on Non-socket */

    Enotconn/* Socket is not connected */

    Reference

    Big difference between shutdown and close on a socket was thebehavior when the socket was shared by other processes. A shutdown () affects all copies of the socket Whileclose () affects only the file descriptor in one process.

    Even if you close () a TCP socket, it won ' t necessarily beimmediately reusable anyway, since it'll be is in a time_waitstate While the OS makes sure there's no outstandingpackets that might get confused as new information if you were to immediate Lyreuse. Socket for something else.

    Note

    When shutdown closes the Read section, it discards any data in the receive buffer and closes the connection to that end, and if the write part is closed, TCP sends the remaining data and terminates the connected write end.

    4. TCP Status Change graph:



    Several state resolutions:

    (1) Time_wait status

    This state is also known as the 2MSL wait state, which is the maximum lifetime of a message segment, which is the maximum amount of time a message is discarded before it is dropped in the network. So why wait for twice times the MSL? This is because after the TIME_WAIT state, you need to perform an active shutdown, send an ACK, and also need to add one more MSL, in order to wait for the other side of the feedback (whether to receive the re-issued Fin), this is because after sending an ACK, there may be many reasons for the ACK send failed, At this point the server side sends fin here.

    Normally, the client will not be able to use the corresponding socket during 2MSL, but in a specific implementation (such as Berkeley), this interface can be reused through the SO_REUSEADDR option.

    (2) Fin_wait_2 status

    When the other party confirms the fin that he sends, it will enter the Fin_wait_2 state.

    (3) close_wait status and fin_wait_1 status

    When a party in the connection receives the fin from the other side, it enters the close_wait state and the other end enters the fin_wait_1 state. Www.2cto.com

    5. Traffic control mechanism in TCP--sliding window

    Affected by a number of factors, such as hardware (two-card throughput differences), network environment, the network is prone to a variety of congestion, the current measures are mainly the following two kinds: improved congestion algorithm and control the sending and receiving end of the traffic. This section focuses on how to control traffic at the receiving and sending ends.

    (1) Sliding window--receiving end

    Before explaining the sliding window protocol, we can review the classical algorithm-stop-wait algorithm, which was put forward to implement traffic control at the receiving end, and the core idea is that the receiving end receives a datagram and stops receiving the new datagram until the ACK (acknowledgement of the received datagram) is issued. Algorithm thought and implementation is very simple, but encountered an efficiency problem, especially with the network equipment data processing ability has been greatly improved, inefficient is particularly obvious, and later people also tried a variety of improvement measures, such as this section of the sliding window is one of them.

    The basic principle is that there is a receive buffer at the receiving end, which is used to receive data from the sender, and only when the application process takes the data from the receiving buffer (which may only be part) and sends out its ACK, it is counted as this part of the data has been received, and then adjusts the sliding window size at this time. The sender calculates the size of the data that can be sent based on the size of the returned window. Therefore, it can be understood that the sliding window algorithm is the receiver as the active party according to its own cache and processing ability to actively adjust the other side of the transmission of the flow of an adjustment algorithm.

    Below, a sliding window model is shown to understand how the sending party handles the received sliding window.



    In the sender there is still a buffer (send buffer), the data sent can be the following several states:

    Send and confirm (1-3)

    Sent but not confirmed (4-6) www.2cto.com

    can send (7-9)

    Not able to send (after 10)

    It is important to note that the sliding window is based on the received confirmation sequence number. The sender moves backwards and updates the data status accordingly, based on the acknowledgment Sequence number and window size received.

    (2) Sliding window--Send side (congestion window)

    The cause of network congestion is manifold, in addition to sending and receiving two segments of the hardware differences, but also related to network communication links, such as the communication link in the forwarding router cache, imagine such a situation, if the sender and receiver processing capacity and throughput is very strong, if it is only through the receiving end of the sliding window size returned, It is difficult to prevent the datagram from being discarded as a result of blocking in the router's forwarding process, because routers connected to the sending end are unable to store and forward so much data because of their own buffer space. So how can this be avoided? The best mechanism is that the middle router is also involved in sending feedback to the size of the window, which is also the congestion window described in this section.

    Combined with the above, the sender will receive two window sizes, each from the receiving and intermediate routers, noting that the former will appear in each datagram, while the latter is only sent by the intermediate router when congestion occurs in the network. The sender then takes the window size of the receiving end and the minimum value in the Congestion window as the upper limit.

TCP protocol detailed (theory)

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.