#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    3
    Rep Power
    0

    recvfrom() never ending


    I have some code in development for sending a dgram packet to a server and waiting for a reply. When I recvfrom, but there is no data back from the server then the program waits for ever! Is there some way I can timeout the recvfrom()?

    I've included the source in a zip for anyone who's interested.

    Simon M
    Attached Files
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Ok, I added a timeout handler for you. Enjoy :)
    Attached Files
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    3
    Rep Power
    0
    Thank you!!

    You have made my day =)
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Why don't I have half of those libraries? I have the full version of MSVC++ too..
    hmmm...
  8. #5
  9. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Actually, those are just references to header files. -And- this is UN*X code (I test-compiled it on my Linux box). I've heard that on win32, you can simply use something like
    Code:
    #include <winsock.h>
    to do TCP/IP socket programming. When in doubt, look here.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    Another good getting-started reference under Visual C++ is "Transitioning from UNIX to Windows Socket Programming" by Paul O'Steen at http://cs.baylor.edu/~donahoo/pract...dowsSockets.pdf . In it, he gives instructions on converting a UNIX sockets program to a Win32 Winsock console application. Just be aware that while you can do multithreading in both UNIX and Win32, the function names are different. Also, you cannot do process forking or signalling under Windows, but Win32 has Sleep() now.

    Analyzer, I'm just starting to learn UNIX programming, so I'll keep that use of SIGALRM in mind, as well as the use of sigsetjmp() and siglongjmp(). Thanks.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222

    A Solution for Both Worlds


    Another solution to this recvfrom() problem would be to use non-blocking sockets. This solution will work both under UNIX and under Winsock.

    This example is in Winsock (hence the WSA... names):

    Code:
        #define RECV_TIMEOUT 10	/* timeout in seconds */
    
        SOCKET sock;                        /* Socket descriptor -- int in UNIX */
        unsigned long nonblocking = 1;    /* Flag to make socket nonblocking */
        int timeout;
    
        /* Set the socket to nonblocking */
        /* fcntl(sock, F_SETFL,O_NONBLOCK|FASYNC) under UNIX */
        if (ioctlsocket(sock, FIONBIO, &nonblocking) != 0)
            DieWithError("ioctlsocket() failed");
    ...
    
        /* Receive a single datagram from the server */
        for (timeout=RECV_TIMEOUT; timeout > 0; timeout--)
        {
            /* recvfrom() returns -1 if error */
            if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0)
            {
                if (WSAGetLastError() != WSAEWOULDBLOCK) 
                  /* EWOULDBLOCK in UNIX */
                    DieWithError("recvfrom() failed");
                else
                {
                    printf("Still have not received packet...Waiting and then trying again\n");
                    Sleep(1000);  /* Sleep for 1 second */
                }
            }
            else  /* have received datagram */
                break;
        }
    
        if (!timeout)
        {
            printf("Timed out.\n");
            exit(1);
        }
    
        /* process received datagram  */
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    3
    Rep Power
    0
    someone told me in comp.unix.programmer that it could be done with poll() although I couldn't find anything simple enough for me to understand about it! Just another idea into the pool

IMN logo majestic logo threadwatch logo seochat tools logo