Thread: recvfrom() timeout

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    recvfrom() timeout

    is there a simple way to set a timeout on the recvfrom() function?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, using the select() function.

    EDIT:
    Here is a site with a good explanation on its use.
    http://www.lowtek.com/sockets/select.html
    Last edited by bithub; 09-10-2004 at 01:07 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I found an example I like better than the one In the link I posted:

    Code:
    SOCKET     theSocket;
    struct timeval stTimeOut;
    
    fd_set stReadFDS;
    fd_set stExcepFDS;
    fd_set stWriteFDS;
    
    FD_ZERO(&stReadFDS);
    FD_ZERO(&stExcepFDS);
    FD_ZERO(&stWriteFDS);
    
    // Timeout of one second
    stTimeOut.tv_sec = 1;
    stTimeOut.tv_usec = 0;
    
         FD_SET(theSocket, &stReadFDS);
         FD_SET(theSocket, &stExcepFDS);
         FD_SET(theSocket, &stWriteFDS);
    
              int test = 0;
              char szBuf[256];
              memset(szBuf, ' ', sizeof(szBuf));
    
              while (true) {
                   int t = select(-1, &stReadFDS, &stWriteFDS, &stExcepFDS, &stTimeOut);
                   if (t = SOCKET_ERROR) {
                        fprintf(stderr, "Call to select() failed");
                        exit(1);
                   }
                   if (t != 0) {
                        printf("Something to do...");
                        if (FD_ISSET (theSocket, &stExcepFDS)) {
                             printf("Exception flag is set");// Deal with this
                        }
                        if (FD_ISSET(theSocket, &stReadFDS)) {
                             printf("There is data pending to be read..."); // Read data with recv()
                        }
                        if (FD_ISSET(theSocket, &stWriteFDS)) {
                             printf("There is data pending to be written...");// Send Data with send()
    
                        }
                   }
                   else {
                        printf("t is zero");
                        exit(1);
                   }
              }

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i've been reading on select() and i do understand the "reading" part, but what's with the "writing" and "exception"?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    For what you are looking for, you only care about reading.

    Change the select line to:
    select(-1, &stReadFDS, 0, 0, &stTimeOut);

    Then if select returns a number greater than 0, there is data to be read from the socket. If select returns 0, there was nothing to read and the call timed out.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since when was passing -1 to select a good idea?
    It's supposed to be the max file descriptor in all the sets, plus 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Read the MSDN docs:
    http://msdn.microsoft.com/library/de...k/select_2.asp

    In winsock, that parameter is ignored. Since the poster didnt specify a platform, I assumed windows (as is evident by the SOCKET structure in my example).

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    well i read the msdn, but isn't writefds kinda stupid? because i can just check if connect() failed with and if statment, no?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You dont need to check for writefds. Read my last post

    For what you are looking for, you only care about reading.
    Change the select line to:
    Code:
    select(-1, &stReadFDS, 0, 0, &stTimeOut);

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i understand that, but still when will i need it?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well keep in mind that select() works on more than just sockets. It can also work on regular file descriptors as well.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    MS select does not work on non-socket file descriptors
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    ok, but the standart berkley select() does? can you give me an example?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > can you give me an example?
    I'd be surprised if a board search didn't turn up at least one example
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recvfrom - fromlen parameter
    By symbiote in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-21-2009, 02:52 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. timeout for recvfrom()
    By nkhambal in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-06-2006, 01:54 AM
  4. rsh timeout
    By jlai in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 07:18 AM
  5. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM