NO

Author Topic: Sleep() after send()  (Read 3030 times)

liut

  • Guest
Sleep() after send()
« on: July 29, 2013, 10:27:50 AM »
Hello,

in my winsock server program I use the following codes to send something to the client-end:

ioctlsocket(fd,...);    // Enable the blocking mode. The fd attribute is inheritted from the main fd in the main listener set by non-blocking mode

setsockopt(fd,...);    // 3 lines of codes, to set time-out for recv() / send(), and "LINGER" mode with time-out for closesocket()

send(fd,...);
send(fd,...);
...

Sleep(100);    // I have to set this before closesocket()

closesocket(fd);

--------------------------

If I don't the "Sleep()", sometimes the client-end may can't receive all data and lost connection under an unstable state. If I always use it, everything looks smooth but it causes the thread delaying a little to exit. In some other places, I always needn't use it before closesocket() to avoid such issue. however in those codes, I just send more or less data without any setting changed during the transmitting.
This issue bores me and I can't find out the answer. Please feel free to comment. Thanks!

liut

  • Guest
Re: Sleep() after send()
« Reply #1 on: August 20, 2013, 12:00:50 PM »
I finally found out the root cause: I close this socket without reading any incoming data from buffer yet. At other places, I always read data from buffer and do something and then close the socket. After I add the code to read data before closing the socket, no more such issue. ;D