Socket Timeout

Posted on 2006-04-18
Last Modified: 2008-03-04

i wrote a TCP Server with C++ (Linux)...

my question is how can i handle a timeout in the recv function?

as example if my server stand in the recv function and the client dont respronse for 1 minute i want to close the connection.

how can i do this?
Question by:woigl
    LVL 12

    Expert Comment

    Use select before recv.

    Author Comment

    Can you give me example source?
    LVL 12

    Expert Comment

    LVL 12

    Accepted Solution

    Something like this

    int fd = -1; // This is the socket you have created.

    fd_set readset;
    struct timeval tv;
    int ret = -1;
    // Initialize the set
    FD_SET(fd, &readset);
    tv.tv_sec = 1;
    tv.tv_usec = 0;

    ret = select (FD_SETSIZE, &readset, NULL, NULL, &tv); // Wait for one second

    if (ret == 0) // Timeout
    else if (ret < 0) // Some error
          // Use recv
    LVL 39

    Expert Comment

    FYI: select can check more than one read socket at a time. You would have to call FD_SET for any socket you want to include.

    An alternative method than using select is to set the socket to non-blocking. Then, a call to recv would return immediately, normally with SOCKET_ERROR that indicates that there is nothing to read.

        // set the socket to non-blocking
        unsigned long argp = 1;
        ioctlsocket (sock, FIONBIO, &argp);

       int timeoutCounter = 0;
       int timeout            = 10;  // 10 milliseconds
       while (timeoutCounter < 1000 / timeout)            
            int bytes = recv(sock, buffer, sizeof(buffer)-1,0);
            if (bytes == SOCKET_ERROR)
                // nothing to read
                if (WSAGetLastError() == WSAEWOULDBLOCK)
                   // sleep a little while
                   continue;    // try again
                    cout << "ClientThread: recv error " << WSAGetLastError() << endl;
             timeoutCounter = 0;  // reset counter
             // do something with buffer

    Regards, Alex

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now