• Advertisement

Archived

This topic is now archived and is closed to further replies.

Blocking... for a while

This topic is 6511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve got a thread that contains a looping procedure that does a recvfrom() and places the data on a queue. Trouble is, if my client doesnt recieve data then the thread will never end and the loop will never roll over. What I need is a way to tell recvfrom() to block for 500ms then drop through with a errorcode. That way I can guarantee that if I loose the network connection then the thread won''t hang. I''m sure I read about this somewhere in my early days in winsock but can seem to find it now... Many thanks gimp

Share this post


Link to post
Share on other sites
Advertisement
Not sure if you can specify a delay for the recvfrom call, but you could use select, and specify a timeout. If select returns, and your socket isn''t in the recv set, then treat that as your error condition.

- g

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I believe that if the connection fails recv returns with either a socket error or a read of 0 bytes which indicates an EOF condition for a closed or broken connection. You''ll have to check on this though. So the thread unblocks when the connection fails or is closed and you''ll just have to put code in which handle those condition, no need to implement any complicated escape routines.

Good Luck

-ddn

Share this post


Link to post
Share on other sites

{
fd_set readfds;
struct timeval timeout;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 500000; //microseconds
select(sock+1, &readfds, NULL, NULL, &timeout);
if (FD_ISSET(sock, &readfds)) {
len = recvfrom(sock, buf, bufsize, 0, &addr, &addrlen);
}
}

Share this post


Link to post
Share on other sites
Cool!

Thanks a lot, especially for including code with recvfrom(), I''d hate to have to use that old trick of connect()''ing a UDP socket just to use this

again thanks

gimp

Share this post


Link to post
Share on other sites

  • Advertisement