Three even four broken

Source: Internet
Author: User
Tags ack bit set dashed line connection reset

The establishment of the 1.TCP connection (1) begins with theprocess of server initialization ,starting with the CLOSED (shutdown) state and sequentially invoking the SOCKET,BIND,LISTEN and the ACCEPT primitive creates a socket socket , enters the LISTEN(listening) state, waits for a TCP transport connection request from the client.

(2) The client starts by calling the socket primitive from the CLOSED state to create a new socket socket , and then calls the CONNECT primitive again , Send a data segment to the server that resets the SYN field to 1(indicating this is a synchronized data segment) , initiating an open port and entering into synsent (theconnection request has been sent Wait for the other party to confirm) status.


Three handshake processes established for TCP transport connections

(3) The server receives a message from the clientSynData segment, send back aSynField Placement1ACK< Span style= "FONT-SIZE:18PX;" > field 1 (confirmation number) =i+1 The answer data segment (assuming the initial sequence number is jsyn rcvd (a connection request was received but not confirmed) status. Note here that the confirmation number is i+1 , instead of I i+1

      (4 syn+ack After the data segment, send a ack=1 i+1 ,ack=j+1 confirmation data segment, At the same time enter established (connection established) State, establish one-way connection. Note that the at this point is i+1 , confirmation number is j+1 j+1

(5) The server receives the client's ACK data segment, enters the established state, completes the two-way connection establishment.

TCP Connection establishment process for both sides simultaneously active connectionnormally, the transmission connection is initiated by one party, but it is also possible for both parties to initiate the connection at the same time, there will be a connection collision, eventually only a connection can be established. Because all connections are identified by their endpoints. If the first connection request establishes a connection identified by the socket (x, y), and the second connection establishes such a connection, there is only one socket table entry inside the TCP entity.

When a connection request is made at the same time, the two ends are sending almost oneSynField Placement1Data segment, and enter theSyn_sentState. When each end receivesSynData segment, the state changes tosyn_rcvd field 1,ack field 1syn data segment to confirm. When both parties receive each other's syn+ackestablished state. Figure 10-39 is a tcp connect instead of two Span style= "FONT-SIZE:18PX;" , we should pay special attention to this point.


Figure 10-39 The TCP connection establishment process that initiates the connection at the same time

As can be seen, a two-side open transmission connection needs to Exchange 4 data segments, more than the normal transmission connection to establish a three-time handshake multi-exchange a data segment . It is also important to note that at this point we do not refer to any end as a customer or server, because each side is both a client and a server.


Release of the 2.TCP connection after the data transfer is complete, both sides of the communication can make a request to release the connection. The process for releasing the connection is as shown in: 1) After the data transfer, the application process of host a releases the connection request to its TCP and does not send the data.     TCP notifies the other party to release a connection from A to B, the terminating bit of the header of the TCP packet destined for Host B is set to 1, and the ordinal U equals the ordinal of the last byte of the transmitted data plus 1. 2) Host B TCP receives the release connection notification after the acknowledgment, its ordinal number is u+1, and notifies the application process, so that a to B connection is released, the connection is in a semi-closed state.     Host B is not accepting data from host A, but Host B also sends data to a, and host a will still need to send confirmation if it receives the data correctly. 3) After Host B sends the end of data to host a, the application process notifies TCP to release the connection.     The connection release message segment issued by Host B must have the terminating bit set to 1 and the ordinal w equal to the ordinal of the last byte of the previously transmitted data plus 1, and the last sent ack=u+1 must be repeated. 4) host A to Host B connection Release message section issued a confirmation, the ACK is set to 1,ack=w+1, Seq=u+1. In this way, the opposite connection from B to A is released, and TCP from host a reports to its application process, and the entire connection is released.2. the TCP connection release process that is actively closed by both parties

As with the simultaneous establishment of a TCP transport connection, the TCP transport connection can be closed by both parties simultaneously (normally the first fin data segment is sent by one side for active connection shutdown, the other side passively accepts the connection is closed), 10-41 is shown. Described below:


Process of actively shutting down TCP connections at the same time

when the corresponding network application layer process calls the close primitive at the same time, when the fin data segment is sent to execute the close command, both ends are shifted from the established state to the Fin WAIT 1 state. When either party receives the fin data segment sent to the end, its status is shifted from Fin WAIT 1 to the closing state and the last ACK data segment is sent. When the last ACK data segment is received, the state transitions to time_wait, after waiting for 2MSL to enter the closed state, eventually releasing the entire TCP transport connection. 3. Issues of attention
  • Three times the need for the sender to send confirmation again when a connection is established
    • The main purpose is to prevent the failed connection request message segment from suddenly uploading to B, resulting in an error. Suppose that an exception occurred, that is, the first connection request packet sent by a segment is not lost, but in some network nodes for a long time, has been delayed until the connection is released some time after the arrival of B, this is a long overdue message segment. However, after receiving this invalid connection request packet, B is mistaken for a and a new connection request, so send a confirmation message to a section, agree to establish a connection. Assuming that the three-time handshake is not used, the new connection is established as soon as B confirms, so that a lot of resources for B are wasted.
    • The three-time handshake is a sufficient and necessary condition for the correct synchronization of the two ends of the connection because TCP is based on unreliable packet transport services, which can be lost, delayed, duplicated, and scrambled, so the protocol must use a timeout and retransmission mechanism. If a retransmission connection request and the original connection request arrive at the time the connection is being established, or a deferred connection request arrives after a connection has been established, used, and ended, a problem occurs. These problems can be solved with a three-time handshake protocol. The ACK data segment sent by the client is intended to avoid a duplicate connection due to network latency, because the client can then know whether the connection request has been invalidated by checking the ACK number in the acknowledgment data segment.
  • Four times to wave and release the connection, wait for the meaning of 2MSL
    • 1) reliable termination of TCP full-duplex connection         first, in order to ensure that a sends the most one ACK message segment to reach B. This ACK segment may be lost so that B in the Last-ack state does not receive confirmation of the sent fin and ACK segments. B will time out to retransmit the fin and ACK segment, and a will receive the retransmission Ack+fin message segment within 2MSL time. Then a re-transmit confirmation. When the four-way handshake protocol is closed, the final ACK is issued by the active shutdown, and if the final ACK is lost, the server will re-send the final fin, so the client must maintain state information to allow it to re-send the final ack. If this status information is not maintained, then the client responds to the RST sub-section, which the server interprets as an error (Connection Reset's socketexception is thrown in Java). Therefore, to achieve the normal termination of TCP full-duplex connection, you must handle the loss of any one of the four sub-sections of the termination sequence, the active shutdown client must maintain state information into the TIME_WAIT state.

    • TCP sub-section may be due to router anomalies and "lost", during the lost, the TCP sender may be due to confirm time-out and resend the sub-section, the Lost sub-section after the router repair will also be sent to the final destination, this original stray section is called lost Duplicate. After a TCP connection is closed, a TCP connection between the same IP address and port is immediately re-established, and the latter connection is called the avatar of the previous connection (incarnation), it is possible that the previous connection's stray repeat group appears after the previous connection has terminated. Thereby being misunderstood to belong to the new incarnation. To avoid this situation, TCP does not allow a connection in the TIME_WAIT state to start a new avatar, because the TIME_WAIT state continues to 2MSL, which guarantees that when a TCP connection is successfully established, the repeating grouping from the previous avatar is lost in the network.

    • A short number of requests may cause a lot of time_wait on the server, so the port is exhausted and the application cannot continue to be accessed. The four-time handshake shutdown is also not required, and the socket option setting does not require attention to the TIME_WAIT state.

The steps required for the establishment and release of a 4.TCP finite state machine connection can be expressed in a finite state machine with 11 states. There are some legitimate events in each state, and when a legitimate event occurs, you may need to take an action. When other events occur, an error is reported.

State

Description

CLOSED

Off state, no connection activity, or in progress

LISTEN

Listening status, the server is waiting for the connection to enter

SYN RCVD

Received a connection request, not yet confirmed

SYN SENT

Connection request has been made, waiting for confirmation

Established

Connection established, normal data transfer status

FIN WAIT 1

(active shutdown) has sent a shutdown request waiting for confirmation

FIN WAIT 2

(active close) receive close confirmation from each other, waiting for each other to close the request

TIMED WAIT

Complete two-way shutdown, waiting for all packets to die

CLOSING

Both sides try to close simultaneously, waiting for the other party to confirm

CLOSE WAIT

(passive off) received close request, confirmed

Last ACK

(passive off) wait for the last shutdown acknowledgement and wait for all packets to die

Changes in TCP establishment and release:

      • Process of customer process change (thick solid line)
        • Connection establishment: A host client process initiates a connection request (active open), when the local TCP entity creates the Transfer Control Fast (TCB), sends a SYN 1 message, and enters the syn_sent state. When a SYN and ACK from the process is received, TCP sends out the last ACK in the three handshake to enter the state established the connection has been established.
        • Connection release: Sets the client process host local TCP entity to send a fin to 1 message waiting to confirm the arrival of the ACK, when the state becomes fin_wait_1. When the client process host is running to receive an ACK acknowledgement, the connection in one Direction is closed. The state becomes fin_wait_2. When the host running the client process receives a message that the host that is running the server process sends a FIN of 1, it should respond with the acknowledgment ACK, which is another connection shutdown. At this point, however, TCP waits for a period of time before deleting the original established connection record. Returns to the initial closed state, which is to ensure that all the groupings on the original connection have disappeared from the network.
      • Process of server process transition (dashed line)
        • Connection established: The server process is issued passive open, into the listening state listen. After receiving the connection request message with the SYN set to 1, the acknowledgment ACK is sent, and the SYN in the message is also set to 1 and then enters the SYN_RCVD state. When the last acknowledgment ACK is received for three handshakes, it is converted to the established state.
        • Connection release: When the data for the client process has been transferred. Sends a message to the server process with a fin set of 1 and enters the close_wait state. The server process sends the FIN message segment to the client process, and the status changes to the Last_ack state. When an ACK is received from the client process, the server process releases the connection. Deletes a connection record. Back to the original closed state

Three even four broken

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.