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
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, ©, 0, 0, &timeout);
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With