Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of select() for timeout

I was only able to set maximum of 20 seconds as the timeout parameter in select () API. Whatever value i gave above 20, select() is returning after 20 seconds itself... So i was trying to write a loop for the timeout of 1 minute like this

    int timeoutcount = 0;
    do
    {
    FD_ZERO(&fd);
    FD_SET(sock,&fd);
    timeout.tv_sec = 20;
    timeout.tv_usec = 0;
    rc = select (sock+1,&fd,null,null,&timeout);
    if(rc ==0)
    timeoutcount += 20;
    }
    while(rc ==0 && timeoutcount <60)

please help me out...am i going in the correct way? If so,select returns 1 after first timeout..help me figure this out too Note: i'm using it in objective C

like image 331
Kesav Avatar asked Mar 21 '12 05:03

Kesav


People also ask

How do you use select timeout?

select() timeout If we want select() to wait a maximum of 1.5 seconds, we can call it like this: struct timeval timeout;timeout. tv_sec = 1;timeout. tv_usec = 500000;select(max_socket+1, &copy, 0, 0, &timeout);

What does select () do in socket?

The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

What does select () do in C?

The select() function tests file descriptors in the range of 0 to nfds-1. If the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read.

What occurs when the timeout value set for a socket has been exceeded?

If the timeout elapses before the method returns, it will throw a SocketTimeoutException. Sometimes, firewalls block certain ports due to security reasons. As a result, a “connection timed out” error can occur when a client is trying to establish a connection to a server.


1 Answers

There is no 20-second maximum for the timeout to select -- something else (most likely data being ready-for-read on your socket) must have been causing select() to return early. If you really only want to use select() as a way to sleep, try calling it like this:

struct timeval tv = {600, 0};   // sleep for ten minutes!
if (select(0, NULL, NULL, NULL, &tv) < 0) perror("select");
like image 153
Jeremy Friesner Avatar answered Sep 28 '22 08:09

Jeremy Friesner