TCP Status: LISTEN: Listening for connection requests from a remote TCP port
Syn-sent: Wait for a matching connection request after sending the connection request again
Syn-received: Wait for confirmation of connection request after receiving and sending a connection request
Established: Represents an open connection
Fin-wait-1: Waiting for a remote TCP connection interrupt request, or confirmation of a previous connection interrupt request
Fin-wait-2: Wait for connection interrupt request from remote TCP
Close-wait: Waiting for a connection interrupt request from a local user
CLOSING: Waiting for remote TCP to confirm connection interruption
Last-ack: Wait for confirmation that the original connection to the remote TCP interrupt Request
Time-wait: Wait enough time to ensure that remote TCP receives a confirmation of a connection interrupt request
CLOSED: No connection statusTCP is a connection-oriented protocol, so you need to first establish a connection before connecting the two sides to send data. This is completely different from the agreement mentioned earlier. All of the previous protocols are just sending data, most of them do not care about sending the data is not sent, UDP is particularly obvious, from the point of view of programming, UDP programming is also simpler than----UDP do not consider data fragmentation. The book uses Telnet login to explain the process of establishing and stopping TCP protocol connection, you can see that the establishment of TCP connection can be simply called three times handshake , and the termination of the connection can be called four times handshake . 1. Establishment of Connection When the connection is established, the client first requests to the server to open a port (with the SYN segment is equal to 1 of the TCP message), and then the server sends back an ACK message notification client request message received, the client received a confirmation message again after the confirmation message to confirm just the server sent a confirmation message (around the mouth Mody) , at this point, the establishment of the connection is complete. This is called three times handshake. If you intend to make both sides ready, be sure to send a message three times, and only three messages will be required. As you can imagine, TCP can guarantee that a packet will be sent to the destination if it is coupled with TCP's timeout retransmission mechanism. 2. End Connection TCP has a particular concept called half-close, which is that TCP connections are Full-duplex (which can be sent and received at the same time), so the connection must be turned off and sent in two directions when the connection is closed. The client gives the server a TCP message with a fin of 1, then the server returns a confirmation ACK message to the client and sends a FIN message, and when the client replies the ACK message (four times), the connection ends. 3. Maximum message length When a connection is established, the two sides of the communication have to confirm each other's maximum message length (MSS) for communication. Generally this SYN length is the MTU minus the fixed IP header and TCP first ministerial degree. For an Ethernet, it can generally reach 1460 bytes. Of course, for Non-local IP, this MSS may be only 536 bytes, and this value will become smaller if the MSS of the intermediate transmission network is a little bit better. 4.TCP State Migration Diagram The P182 page gives a state diagram of TCP, this is a seemingly complex state migration diagram, because it contains two parts---the server's state migration and the client's state migration, if from a certain point of view this picture, it will be much clearer, which server and client are not absolute, The data is sent by the client, which is the server that accepts the data. 4.1. State migration diagram for client applications The state of the client can be represented by the following process: Closed->syn_sent->established->fin_wait_1->fin_wait_2->time_wait->closed The above process is in the normal situation of the procedure should have the process, from the book can be seen in the figure, when the connection is established, when the client received the SYN message ACK, the client opened the data interactively connected. The end of the connection is usually the client-side active end, the client end the application, the need to experience fin_wait_1,fin_wait_2, and so on, these state migration is mentioned above the end of the connection four times handshake. 4.2. Server's state migration diagram The state of the server can be represented by the following process: Closed->listen->syn received->established->close_wait->last_ack->closed When a connection is established, the server side enters the data interaction state after a third handshake, and the closing connection is after the second handshake of the connection is closed (note that it is not the fourth time). And after the shutdown will also wait for the client to give the final ACK packet to enter the initial state. 4.3. Other State Migration The diagram in the book also has some other state migrations, which are summarized as follows for both the server and the client Listen->syn_sent, for this explanation is very simple, the server sometimes to open the connection. Syn_sent->syn received, the server and the client in the Syn_sent state if the SYN datagram, you need to send SYN ACK datagram and adjust their status to SYN received status, ready to enter the established syn_sent- >closed, returns to the CLOSED state when the Send timeout occurs. Syn_ received->listen, if the RST packet, will return to the LISTEN state. Syn_ received->fin_wait_1, this migration is said, you can not go to the established state, but can jump directly to the fin_wait_1 state and wait for the shutdown. 4.4.2MSL Wait Status In the book to the figure inside, there is a time_wait waiting state, this state is also called 2MSL State, said that after the Time_wait2 sent the last ACK datagram, to enter the TIME_WAIT state, This state is intended to prevent the last handshake datagram from being sent to the other side (note that this is not a four handshake, which is the insurance status for the fourth handshake). This state is largely a guarantee that both sides can end normally, but the problem comes. Because of the 2MSL state of the socket (socket is the meaning of IP and port pair, socket), so that the application in 2MSL time is not able to use the same socket again, for the client program is good, but for service programs, such as httpd, it always use the same port for service, And in the 2MSL time, the boot httpd will have errors (sockets are used). To avoid this error, the server gives the concept of a quiet time, which is to say that, although the server can be restarted during the 2MSL time, the server still waits for the 2MSL time in the past for the next connection. 4.5.fin_wait_2 State This is the state of the famous semi shutdown, which is the state after the client and the server shook hands two times when the connection was closed. In this state, the application also has the ability to accept data, but it has been unable to send data, but there is also a possibility that the client has been in a fin_wait_2 state and the server has been in the Wait_close state until the application layer decides to turn off the state. 5.RST, both open and close at the same time RST is another way to close the connection, and the application should be able to determine the authenticity of the RST package, that is, whether it is aborted. While opening and closing at the same time are two special TCP states, the probability of occurrence is very small. 6.TCP Server Design Previously described UDP server design, you can find that the UDP server does not need the so-called concurrency mechanism, it can only establish a data input queue. However, unlike TCP, TCP servers need to establish a separate process (or lightweight, thread) for each connection to ensure the independence of the dialog. So the TCP server is concurrent. And TCP also needs to be equipped with a call-in connection request queue (the UDP server is also not required) to establish a dialog process for each connection request, which is why each TCP server has a maximum number of connections. Depending on the IP and port number of the source host, the server can easily distinguish between different sessions to distribute the data. |