TCP three-time handshake and four waves (2)

Source: Internet
Author: User
Tags ack

Tag: Check the header flowchart after entering completion to tell get to establish a connection end-to-end

Background description

Through the introduction of the IP layer in the network model in the previous article, we know that the network layer can realize the communication between the two hosts. But this is not specific, because the entity that really communicates is the process in the host, which is a process in a host that exchanges data with a process in another host. Although the IP protocol can send the data message to the destination host, it is not delivered to the host for the specific application process . The end-to-end communication should be the communication between application processes.

UDP, there is no need to establish a connection before transmitting the data, and the remote host does not need to give any confirmation after receiving the UDP message. Although UDP does not provide reliable delivery, but precisely because of this, the omission and a lot of overhead, making it faster, such as some of the high-performance requirements of the service, often using UDP. The corresponding application layer protocol mainly has DNS,TFTP,DHCP,SNMP,NFS and so on.

TCP, which provides a connection-oriented service, must establish a connection before the data is transferred, releasing the connection after the data transfer is complete. TCP is therefore a reliable transport service, but because of this, it inevitably adds a lot of overhead, such as confirmation, flow control, and so on. The corresponding application layer protocol mainly has smtp,telnet,http,ftp and so on.

Common well-known port numbers
Application FTP TFTP TELNET SMTP DNS HTTP SSH MYSQL
Well-known ports 21,20 69 23 25 53 80 22 320W
Transport Layer Protocol Tcp Udp Tcp Tcp Udp Tcp
Overview of TCP

TCP connects the connection as the most basic object, each TCP connection has two endpoints, which we call a socket (socket), which is defined as the port number stitching to the IP address constitutes a socket, for example, if the IP address is 192.3.4.16 and the port number is 80, Then the resulting socket is 192.3.4.16:80.

TCP Message Header
  1. Source and destination ports, each accounting for 2 bytes, respectively written to the source port and destination port;
  2. The ordinal number, which is 4 bytes,is numbered sequentially for each byte in the stream of bytes transmitted in the TCP connection . For example, the ordinal field value of a message is 301, and the data that is carried is 100 fields, and it is obvious that the next message segment (if any) should start at 401.
  3. The confirmation number, which accounts for 4 bytes, is the ordinal of the first data byte expected to receive the next message from the other . For example, B received a message sent by a, its serial number field is 501, and the data length is 200 bytes, which indicates that B correctly received a sent to the ordinal 700 of the data . Therefore, B expects to receive the next data sequence number of a of 701, and B to set the confirmation number to 701 in the Acknowledgment message section sent to A;
  4. Data offset, which accounts for 4 bits, indicates how far the data of the TCP packet is from the beginning of the TCP message segment;
  5. Reserved, accounted for 6 bits, reserved for future use, but should now be bit 0;
  6. Emergency Urg, when urg=1, indicates that the emergency pointer field is valid. Tell the system that there are urgent data in this message section;
  7. Confirm Ack, the confirmation Number field is valid only when ack=1. TCP stipulates that the transmission of all messages after the connection is established must be set to 1 ack;
  8. Push PSH, when two application process interactive communication, sometimes at one end of the application process want to type a command immediately after the response of the other side, this time will be psh=1;
  9. Reset rst, when Rst=1, indicates a serious error in the TCP connection, must release the connection, and then re-establish the connection;
  10. Synchronous SYN, used to synchronize the sequence number when the connection is established. When the syn=1,ack=0, indicates that the connection request message, if the consent connection, the response message should make syn=1,ack=1;
  11. Terminates fin, used to release the connection. When Fin=1, the data indicating that the sender of this message has been sent and is required to be released;
  12. window, accounting for 2 bytes, refers to the notification receiver, send the paper you need to have how much space to accept;
  13. Inspection and, accounting for 2 bytes, calibration header and data of the two parts;
  14. The emergency pointer, which accounts for 2 bytes, indicates the number of bytes of emergency data in the section of this newspaper;
  15. Options, which are variable in length, define some other optional parameters.
Establishment of a TCP connection (three-time handshake)

At the very beginning, both the client and the server are in the closed state. The active open connection is for the client, and the passive open connection is the server.

    1. The TCP server process first creates the transport control block TCB, ready to accept the client process's connection request, when the server enters the listen (listening) state;
    2. The TCP client process also creates the transport control block TCB and then sends a connection request message to the server, which is the same location syn=1 in the header of the message and selects an initial sequence number seq=x, at which point the TCP clients process enters the syn-sent (synchronous sent state) state. TCP Specifies that the SYN message segment (syn=1 segment) cannot carry data, but consumes a sequence number.
    3. When the TCP server receives the request message, it sends a confirmation message if it consents to the connection. Confirm that the message should be ack=1,syn=1, the confirmation number is ack=x+1, but also to initialize a serial number for themselves seq=y, at this time, the TCP server process entered the SYN-RCVD (synchronous received) state. This message cannot carry data, but it also consumes a serial number.
    4. After the TCP client process receives the acknowledgement, it also confirms to the server. Confirm the ack=1,ack=y+1 of the message, its own serial number seq=x+1, at this time, the TCP connection is established, the client enters the established (established connection) state. TCP Specifies that the ACK segment can carry data, but the serial number is not consumed if the data is not carried.
    5. When the server receives confirmation from the client, it also enters the established state, after which the two parties can begin to communicate.
Why does the TCP client send a confirmation last time?

In a word, the main prevention of the failure of the connection request message suddenly transmitted to the server, resulting in an error.

If you are using a two-time handshake to establish a connection, suppose there is a scenario where the client sends the first request connection and is not lost, just because the network node is stuck for too long , because the TCP client has not received a confirmation message that the server did not receive, This message is then re-sent to the server, after which the client and server have two handshakes to complete the connection, transfer the data, and then close the connection. At this time, the previous stranded request connection, the network has reached the server, the report text should be invalid, but the two-time handshake mechanism will allow the client and the server to establish a connection again, which will lead to unnecessary errors and waste of resources.

If the three-time handshake is used, even if the message was sent to the expiration, the server received the failure message and replied to the confirmation message, but the client does not issue a confirmation again. Because the server cannot receive the acknowledgement, it knows that the client has not requested a connection.

Release of the TCP connection (four waves)

After the data transfer is complete, both sides can release the connection. At first, both the client and the server are in the established state, and then the client shuts down actively and the server shuts down passively.

  1. The client process sends a connection release message and stops sending data. Release the data packet header, Fin=1, whose sequence number is seq=u (equal to the last byte of the previously transmitted data sequence number plus 1), at this point, the client enters the state of fin-wait-1 (terminating wait 1). TCP Specifies that the fin segment consumes a sequence number even if it does not carry data.
  2. The server receives a connection release message, sends a confirmation message, ack=1,ack=u+1, and takes its own serial number seq=v, at which point the service side enters the close-wait (shutdown wait) state. The TCP server notifies the high-level application process, the client to the server direction is released, this time in the semi-closed state, that the client has no data to send, but the server if the data sent, the client will still accept. This state also lasts for some time, that is, the duration of the entire close-wait state.
  3. When the client receives a confirmation request from the server, the client enters the fin-wait-2 (terminating wait 2) state and waits for the server to send the connection release message (which will need to accept the last data sent by the server).
  4. After the server sends the final data, it sends a connection release message to the client, fin=1,ack=u+1, because in the semi-closed state, the server is likely to send some data, assuming that the serial number is seq=w at this time, the server entered the Last-ack (final confirmation) state, Wait for the client to confirm.
  5. After the client receives a connection release message from the server, it must issue a confirmation, ack=1,ack=w+1, and its serial number is seq=u+1, at which point the client enters the time-wait (time Wait) state. Note that at this point the TCP connection is not released and must go through the time of 2∗∗MSL (the longest message segment life) when the client revokes the appropriate TCB before it enters the closed state.
  6. As soon as the server receives the acknowledgement from the client, it enters the closed state immediately. Again, after the TCB is revoked, the TCP connection is ended. as you can see, the server ends the TCP connection a bit earlier than the client.

Why does the client have to wait for 2MSL at last?

MSL (Maximum Segment Lifetime), TCP allows different implementations to set different MSL values.

First, to ensure that the client sends the last ACK message can reach the server, because this ACK message may be lost, standing in the server's view, I have sent a fin+ack message request disconnected, the client has not responded to me, should be I sent a request to disconnect the message it did not receive, The server will then resend again, and the client will be able to receive this retransmission message within the 2MSL time period, then give the response message and restart the 2MSL timer.

Second, the "Failed connection request message segment" Mentioned in the "three-time handshake" is prevented from appearing in this connection. After the client sends the last acknowledgment message, in this 2MSL time, all the message segments generated during the duration of the connection will be removed from the network. In this way, the old connection's request message does not appear in the new connection.

Why is it that the connection is three handshakes, and the connection is turned off four times?

When the connection is established, the server sends the ACK and SYN in a message to the client when it receives the SYN message to establish the connection request in the listen state.
When the connection is closed, the server receives the other side of the fin message, only that the other side no longer send data but also can receive data, and they may not all the data are sent to each other, so you can immediately close, you can send some data to each other, then send fin message to the other side to express the consent to close the connection now, Therefore, your own ACK and fin are generally divided into the development of sending, resulting in one more.

What if a connection has been established, but the client suddenly fails?

TCP also has a keepalive timer, obviously, if the client fails, the server can not continue to wait, wasted resources. The server resets the timer after each client request, usually set to 2 hours, and if two hours has not received any data from the client, the server sends a probe segment, which is sent every 75 minutes thereafter. If a 10 probe packet is still not responding, the server considers the client to be faulty and then closes the connection.




If you think this article is good, give a little sponsorship.



TCP three-time handshake and four waves (2)

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.