As a Wi N S O CK programmer, it is usually not necessary to understand the actual T c P state. However, understand how the T c P is better understood, how can Winsock API have an impact on changes in the grassroots protocol. In addition, many programmers will encounter a common problem when closing the socket; the T c p stat around the socket is close to the problem we are most interested in. For each socket, its initial state is C L O S e D. If the client initializes a connection, send a S Y N package to the server while setting the client sleeve system to S Y N _ S E N T. After the server receives the S Y n packet, a "S Y N - A C K" package will be issued. As a client, you need to use a A c K to respond to it. At this point, the client's socket becomes the state of E S TA B L I S HE. If the server does not send the "S Y N - A C K" package, the client will time out and return to the C L O S e d. If a server's socket is binding the same local interface and port, and listens on it, then the status of the socket is L i s t e n. When the client tries to connect, the server will receive a S Y N package and respond with a S Y n - a c k packet. The status of the server socket becomes S Y n _ r C v D. Finally, the client issues a A C K package, which makes the server socket to the state of E S TA B L I S H E D. Once the application is in the state of E S TA B L I S HE E D, it can be closed by two methods. If it is turned off by the application, it is called "Active socket close"; otherwise, the closure of the socket is passive. Figure 7 - 2 explains two closing methods.
If the initiative is closed, the application will send an F I n package. When the application calls C L O S e s O C k E t or S H U T D O W N (the S D_ S e N D is used as a second parameter), a F I n package is issued to the other party, and the state of the socket becomes F i n _ wa i t _ 1. Under normal circumstances, the communication other party will respond to a A c k package, the state of our socket becomes f i n _ wa i t _ 2. If the other party closes the connection, a F i n package will be issued, and our machine will respond to a A C K packet and set the status of the hexic socket to T i m e _ wa i t. Figure 7-2 Turning state TCP socket is also called 2 m s L waiting state. Where M s L represents "Maximum Segment Lifetime", MAXIMUM Segment Lifetime, indicating how long it can be on the network before discarding. Each I p bag contains a "living time" (T t L) field, if it is decremented to 0, the package will be discarded. When each router covered by a package, the T t1 will be reduced by 1, and then continue to pass. Once the application enters the T i M e _ Wa I T state, it will last twice the time of M s1. In this way, T c P can resend it on the premise of the last A c k, that is, F i n will be re-transmitted. After the end of the M s1, the socket enters the C L O S E D after the end of the wait state. When taking active closing measures, there are two paths to enter the T i m e_ WA I T state. In our previous discussion, only one f i n is issued and receives a A c K response. However, the other party can still send data freely until it is also closed. Therefore, two paths are required to play. In a path (ie synchronously turned off), a computer and its communication other party will simultaneously require a shutdown; the computer sends a F i n packet to the other party and receives a F I n packet from it. Subsequently, the computer issues a A c k packet that responds to the other F i n packet and puts its own socket to C L O S i N g. After the computer receives the last A c k packet from the other party, its socket status becomes t i m e _ wa i t. When the active shutdown, the other path is actually synchronous off variants: sockets directly into T i m e _ Wa I T directly from f i n _ Wa I t _ 1 state If the application issues a F I n packet, it is almost simultaneously received a F I N - a C K package there, which occurs. In this case, the other party will confirm that the F i N packet receives the application and send its own F I n pack. For this package, the application responds with a A c k packet. The main function of the T i m e _ WA I T state is that when the T C P connection is waiting for a 2 m s L, it is not necessary to establish a pair of sockets used to establish the connection. This is composed of a sociocal I P port and a remote I P port. For some T C P implementations, they do not allow any port numbers in the socket pair in the T i m e _ Wa I T state. This problem does not exist in Microsoft's solutions. However, if the connection is attempted to establish a connection through a socket that is already in the T i m e _ WA I T state, it will fail and returns W S A e a d R i n u s e error.