establishment, termination, and state transitions of 20160402_tcp connections

Source: Internet
Author: User
Tags ack

Original question:The following status is not part of the TCP connection disconnection?
Time_wait
Fin_wait_1
Sync_sent
Fin_wait_2
Answer:sync_sent
--------------------------------------------------------------------------------Knowledge Point: Computer network

TCP connection establishment:

     The following steps establish a TCP connection:
     1. The server must be ready to accept incoming connections. This is done by calling the socket, bind, and listen functions, called passive open (passive open).
    2. The customer actively opens by calling connect (active Open). This causes the client TCP to send a SYN subsection (for synchronization) that tells the server customer the initial sequence number of the data that will be sent in the (pending) connection. In general, the SYN section does not carry data, it contains only one IP header, a TCP common part, and possible TCP options.
    3. The server must confirm the customer's syn. You also have to send a SYN section that contains the initial sequence number of the data that the server will send in the same connection. The server sends a SYN to the customer and an ACK to the Customer SYN in a single subsection.
    4. The customer must confirm the Syn of the server.

     The connection establishment process requires at least three packets to be exchanged, so it is called a three-way handshake by TCP (Three-way handshake)

Termination of TCP connection:

TCP uses three sub-sections to establish a connection, and terminating a connection requires four sub-sections.

1. An application process first calls close, which we call the active shutdown (active close). TCP At this end sends a FIN section indicating that the data has been sent.
2. Receive the other end of fin and pull the line passively off (passive close). This fin is confirmed by TCP. Its receive is also passed as a file terminator to the receiver application process (after any other data that has been queued for receipt by the application process), because the receive of fin means that the application process will no longer receive additional data on the corresponding connection.
3. After a period of time, the application process that receives the file terminator will call close to close its set of interfaces. This causes its TCP to also send a fin.
4. It is confirmed by receiving the original sender TCP of this fin (that is, the end of the active shutdown).

because a fin and an ACK are required in each direction, four sections are generally required . We use the qualifier "general" because: sometimes the fin of step 1 is sent along with the data, and the ACK from steps 2 and 3 of TCP at the end of the passive shutdown can also be combined into a sub-section with Fin, Between step 2 and step 3 there can be a stream from the execution of the passive shutdown to the execution of the active shut-off side. This is called semi-close (half-close).

When the socket interface is off, each end of TCP sends a fin. This situation occurs when the application process calls close, but when the process terminates, all open descriptors are closed voluntarily (calling exit or returning from the main function) or involuntary (the process receives a signal to terminate the process), and a fin is issued on the still open TCP connection.

TCP state transition diagram:
The establishment and termination of TCP connections can be explained by the state transition Diagram cstate transition diagram).


Figure, 11 states are defined for a connection, and the TCP rule determines how the transition from one state to another is based on the current state and the section received in that state. For example, when an application process executes an active open in the closed state, TCP sends a SYN and transitions from the closed state to the syn_sent state. If the TCP then receives a SYN with an ACK, it sends an ACK and converts it to the es-tablished state . This state is the data transfer state. There are two outward arrows to guide the connection termination processing of the established state. If the app process calls close (active shutdown) before it receives the file terminator, it is converted to the fin_wait_1 state. If the application process receives fin in the established state, it is converted to the ciose_wait state.
The end of the active shutdown enters the TIME_WAIT state, and the end of the passive shutdown enters the close_wait state.


Time_wait Status:

The least understandable part of TCP for network programming is its time_wait state. In the diagram we see the end of the active shutdown entering this state. This endpoint remains in this state for a duration of twice times the maximum section life of MSL (maximum segment lifetime), sometimes called 2MSL.

There are two reasons for the presence of the time-wait state :
1. To achieve the reliability of terminating TCP full-duplex connections
2. Allow old repeating sections to fade out of the network

The first reason:
Assuming that the final response ack is lost, the server will re-send the final fin, so the customer must maintain the status information to allow it to re-send the final ack. If the status information is not maintained, it responds with RST (another type of TCP section), and the server interprets the subsection as an error. if TCP intends to perform all the necessary work to completely terminate a data stream in two directions on a connection (that is, full-duplex shutdown), it must correctly handle the loss of any of the sections in the four sub-sections of the connection termination sequence. This example also shows why one end of an active shutdown enters the TIME_WAIT state because it may have to re-send the final ack.

A second reason:
Let's say 206. 62.226. 33 Ports 1500 and 198.69.10.2 Port 21 has a TCP connection. After we close this connection, we re-establish the same IP address and TCP connection between the ports at some later time . The latter connection is called the avatar of the previous connection (incar-nation). Because they have the same IP address and port number. TCP must prevent old repeating groupings from a connection from reproducing after the connection is terminated, thus being misinterpreted as belonging to the same connection avatar. To implement this feature, TCP cannot start a new avatar for a connection that is in the time-wait state. Since the duration of the time-wait state is 2MLS, it is enough to allow a group in one Direction to live at most msl seconds is discarded, and in the other direction the maximum number of responses to live MSL seconds is discarded. by implementing this rule, we can guarantee that when a TCP connection is successfully established, the old repeating groupings from the previous incarnation of the connection are gone in the network.

establishment, termination, and state transitions of 20160402_tcp connections

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.