TCP/IP status change diagram and TCP three-way handshake and four-way handshake (1)

Source: Internet
Author: User

TCP/IP status change diagram and TCP three-way handshake and four-way handshake (1)

TCP/IP status change diagram:

Detailed descriptions of each status:

CLOSED: indicates the initial state. It is the same for both the server and the C client.

LISTEN: indicates the listening status. The server calls the listen function to start the accept connection.

SYN_SENT: indicates that the client has sent the SYN packet. When the client calls the connect function to initiate a connection, it first sends SYN to the server, then enters the SYN_SENT state, and waits for the server to send ACK + SYN.

SYN_RCVD: indicates that the server receives the SYN Packet sent from the client. After receiving the packet, the server enters the SYN_RCVD status and sends ACK + SYN to the client.

ESTABLISHED: indicates that the connection has been ESTABLISHED successfully. The server enters this status after sending ACK + SYN, and the client also enters this status after receiving ACK.

FIN_WAIT_1: indicates that the connection is closed. No matter which party calls the close function to send the FIN message, it will enter this status.

FIN_WAIT_2: indicates that the passive closing party agrees to close the connection. After the user closes the ACK returned by the user, the user enters this status.

TIME_WAIT: It indicates that the other party's FIN message is received and the ACK message is sent. Then, it can return to the CLOSED state after 2MSL. If FIN_WAIT_1 receives a message with both the FIN mark and ACK mark, it can directly enter the TIME_WAIT status without passing through the FIN_WAIT_2 status.

CLOSING: both parties close the connection at the same time. If both parties call the close function almost at the same time, both parties may send FIN packets at the same time, and the CLOSING status will appear, indicating that both parties are CLOSING the connection.

CLOSE_WAIT: indicates that the passive closing party is waiting to close. When receiving the FIN message sent by the other party by calling the close function, it responds to the ACK message of the other party and enters the CLOSE_WAIT status.

LAST_ACK: indicates that after the passive shutdown Party sends the FIN packet, it waits for the other party's ACK packet status. when it receives the ACK, it enters the CLOSED status.

1. Establish a connection

1) Client

When the Client calls the socket function, the Client generates a socket in the Closed state.

The Client calls the connect function again. The system randomly assigns a port to the Client, along with the parameters (IP address and port of the Server) passed in to connect, which forms a connection triplet, the connect call puts the Client socket in the SYN_SENT state.

When the Server returns confirmation and sends SYN, the Client returns confirmation and SYN, And the socket is in the ESTABLISHED stage, the connection between the two parties can be read and written.

2) Server

When the Server calls the socket function, the Server generates a Closed listening socket. The Server calls the bind operation to associate the listening socket with the specified address and port, then, call the listen function. The system will allocate unfinished queues and complete queues to the listener. At this time, the listener socket can accept the Client connection and the listener socket is in the LISTEN status.

When the Server calls the accept operation, a completed client connection is retrieved from the Completion queue, and a session socket is generated on the server for communication with the client socket, the status of this session socket is ESTABLISH.

2. Close connection

Different from establishing a connection, the connection is divided into server/client, and closing a connection is not an absolute server/client. Closing a connection is divided into active closing and passive closing. Server and client can assume either of these two roles. For example, the client can close its connection with the server, and the same server can also close connections that have not been read or written for a long time. In this case, the following two parts are divided: the client is active and the server is active.

Active Client shutdown, Server passive shutdown:

When a session ends, the Client closes the connection with the Server.

Close rather than shutdown.

When the client wants to close the connection between the client and the server, the client first calls the close function. The client sends a FIN to the server and the client is in the FIN_WAIT1 state. After the server returns the ACK to the client, the client is in the FIN_WAIT2 state and the server is in the CLOSE_WAIT state.

When the server detects the close operation on the client side (read returns 0), the server also needs to call the close operation. The server sends a FIN to the client side. At this time, the server status is LAST_ACK. When the client receives the FIN from the server, the client socket is in the TIME_WAIT status, and it will send an ack confirmation to the server, after the server receives ack confirmation, the socket is in the CLOSED state.

The process of active Server shutdown is similar to that of Client-end shutdown, so we will not talk about it more. Here we also need to pay attention to the status of TIME_WAIT, which is described in the client/server Sections respectively.

First, let's talk about the description of TIME_WAIT described in the TCP/IP explanation and its necessity:

When receiving the FIN operation from the peer end, the socket will be in the TIME_WAIT status, and the socket in the TIME_WAIT status will survive 2MSL (Max Segment Lifetime ),

There is a reason to survive for such a long time:

On the one hand, the end of TCP full-duplex connection is reliably implemented, that is, when the last ACK is lost, the passive shutdown end resends the FIN. Therefore, the active shutdown end must maintain the status information, to allow it to resend the final ACK.

On the other hand, when TCP is waiting for 2MSL, this connection (4 tuples) cannot be used any more, and any late packets will be discarded. Imagine that without the limit of 2 MSL, the new connection exactly meets the original 4 tuples. At this time, the connection may receive the delayed packets on the network and may interfere with the newly established connection. Duplicate segments disappear in the network.


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.