Blocking... for a while

Started by
3 comments, last by gimp 23 years, 10 months ago
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
Chris Brodie
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
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
{   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);   }} 
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
Chris Brodie

This topic is closed to new replies.

Advertisement