Socket Timeout

Hi,

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?
woiglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
Use select before recv.
0
woiglAuthor Commented:
Can you give me example source?
0
rajeev_devinCommented:
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_ZERO(&readset);
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
{
}
else
{
      // Use recv
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
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
               Sleep(timeout);  
               ++timeoutCounter;
               continue;    // try again
            }
            else
            {
                cout << "ClientThread: recv error " << WSAGetLastError() << endl;
                closesocket(sock);
                break;
             }
         }
         timeoutCounter = 0;  // reset counter
         // do something with buffer
         ...
   }

Regards, Alex
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.