Re: how to avoid recv() blocking issue?



You probably want to use event-based socket model
using WSAEventSelect. In your thread you should use
WaitForMultipleObjects with the socket's event and
another event you use to signal your thread to exit.
If you use multiple sockets, you can wait on up to
64 handles within a thread, which allows a thread
to handle up to 63 sockets.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Alexander Cherny" <black@xxxxxxxxxxxxxxx> wrote in message
news:uOAJemnAHHA.4212@xxxxxxxxxxxxxxxxxxxxxxx
interesting. to check data availability before recv() calling. i'll try
it. thank you! :)

although, we have a weak link in a chain: a timeout. i mean, that we can't
stop the thread, until a select()'s timeout expires. is there any way to
stop waiting data in time?

btw, is it possible to make recv() to stop with a timeout by itself?

thanks anyway. it can help.

--
alex c.

"Chris Becke" <chris.becke@xxxxxxxxx> wrote in message
news:O1wISQkAHHA.4292@xxxxxxxxxxxxxxxxxxxxxxx
It is possible to be stuck in a blocking call to recv, and for the server
to
drop the connection abnormally such that the blocking recv will never
exit.
I really dont see how that is happening in this situation.

However, because of that possibility, all my calls to blocking recv use
the
basic code:

first, use ioctlsocket(s,FIONREAD,&cbRead); to check if there is data
available.
If there is, go straight to recv, otherwise call
select(0,fd_s,0,0,&tv_timeout); with a timeout.

"Arkady Frenkel" <arkadyf@xxxxxxxxxxxxxxxx> wrote in message
news:esLEwWjAHHA.2140@xxxxxxxxxxxxxxxxxxxxxxx
That strange because WSAEventSelect() as WSAAsyncSelect do set socket as
non-blocked. Check that you if you used them or through
ioctlsocket() with FIONBIO set to 1 on recv() you have to return with
WSAWOULDBLOCK warning or data
Arkady

"Alexander Cherny" <black@xxxxxxxxxxxxxxx> wrote in message
news:%23Wyf0XfAHHA.4292@xxxxxxxxxxxxxxxxxxxxxxx
hi all,

i got a problem with recv().

there's a thread to receive data from a server. periodically this
thread
has to stop (to let others to do some work). i created some events to
check them inside the thread. the problem happens, when recv() starts
and
does not receive any data from a server. this time the thread is
stuck -
recv() just waits for data, and i can't stop the thread well (not
using
TerminateThread() - btw, it's not a good idea, because a socket
remains
"anchored" by the thread).

is there any way to "unblock" recv() somehow? WSAEventSelect() does
not
help. looks like it's for different purposes.

thanks a lot.

--
alex c.









.



Relevant Pages

  • Re: socket blocks forever if peer machine shuts down or network cable unplug
    ... Put the recv in a do while loop with a switch to check any return messages ... of socket operation... ... > I am reluctant to use non blocking selectwith timeout. ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to avoid recv() blocking issue?
    ... WSAEventSelect() is used to fire windows events, ... WSAEVENT NewEvent = WSACreateEvent; ... now, if any data reading happens, i.e. recv() receives something, NewEvent ... socket will store it in the buffer you gave to receive and assert the event. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Receive no data
    ... It is a best practice to shutdown ... a socket for sending once all data has been sent. ... I see multiple recv() statements in the code. ...
    (microsoft.public.win32.programmer.networks)
  • RE: BUG in Winsock on P4 HT CPU
    ... If you read the help on WSAAsyncSelect it states ... Network transport stack receives 100 bytes of data on socket s and causes ... The application issues recv(s, buffptr, 50, 0) to read 50 bytes. ... on CPU's that do not support HT, or CPUs with HT disabled. ...
    (microsoft.public.win32.programmer.networks)
  • Re: recv() hangs until SIGCHLD ?
    ... Both lsof and ls /proc//fd show that the socket used is in ESTABLISHED mode but when checking on the host on which it's connected we can't find the corresponding client socket. ... We are correctly handling EINTR in sendand recv() by restarting the call in case they get interrupted this way. ... However since this problem does not occur without threads, we can be sure that the blame is still on the receiver. ... In a practical case, we have a thread blocked in recvfor more than 12 hours, which is way beyond the timeout of the sender connection. ...
    (Linux-Kernel)