Write a small one today ProgramWhen a specified message is received, a socket is created to connect to the specified host. Once it is connected, data is sent to and retrieved from the host. To avoid blocking, create a thread every time you need to connect to the host, and call connect () in the thread. After the connection is successful, send () to send some data, then, wsarecv () is called to initiate an asynchronous overlapped request and the thread exits. The result is that the thread responsible for monitoring the completed port does not have data at all, but only receives a socket disconnection event. No way, you have to track the data in one step. The strange thing is that everything works normally during one step tracking. The sent data is sent, and the received data is also received ...... After some debugging information is added, wsarecv () will return wsa_io_pending. The returned value indicates that the overlapping Io has been submitted, but it is not yet completed ...... The returned value is normal, but data is not received. </Div>
The only difference between a single-step trace and a normal operation is that the execution of each statement in a single-step trace is delayed, so wsarecv () we added a sleep (1000) to the front and ran the program. We were surprised to find that the data was received. The wsarecv () value printed by the debugging information was 0 ...... Why can I succeed with latency? I thought for a moment and suddenly came up with a piece of memory a long time ago: When the thread initiating the IO request is terminated, the IO request will be discarded by windows! This is almost certainly the reason for a burst of Ecstasy in my heart. Change it right away. CodeUse queueuserworkitem () and pass the wt_executeiniothread parameter to replace the self-created thread. After running, everything works normally.