dirtdart
asked on
Winsock select
I have a fairly simple Windows TCP/IP client that I am creating. Everything seems to work except trying to call select() to check and see if data is ready for reading on my socket. I'm just using a standard socket with the non-blocking flag set set and the following code:
fd_set set;
FD_ZERO(&set);
FD_SET(connection, &set);
TIMEVAL tv;
tv.tv_sec = timeout;
int err = 0;
switch(opt)
{
case 0:
err = select(0, &set, (fd_set*)NULL, (fd_set*)NULL, &tv);
break;
case 1:
err = select(0, (fd_set*)NULL, &set, (fd_set*)NULL, &tv);
break;
case 2:
err = select(0, (fd_set*)NULL, (fd_set*)NULL, &set, &tv);
break;
}
if((err == 0) || (err == SOCKET_ERROR))
return false;
return FD_ISSET(connection, &set);
When I call the above function, it ALWAYS returns true. When I step through the code, err is set to ERROR_SUCCCESS and FD_ISSSET returns nonzero, even if there is no data on the socket to be read.
I am not stuck using the above solution. Any way that you can suggest to find out if the socket is ready for reading would be fine as long as it doesn not use asynchronous sockets.
fd_set set;
FD_ZERO(&set);
FD_SET(connection, &set);
TIMEVAL tv;
tv.tv_sec = timeout;
int err = 0;
switch(opt)
{
case 0:
err = select(0, &set, (fd_set*)NULL, (fd_set*)NULL, &tv);
break;
case 1:
err = select(0, (fd_set*)NULL, &set, (fd_set*)NULL, &tv);
break;
case 2:
err = select(0, (fd_set*)NULL, (fd_set*)NULL, &set, &tv);
break;
}
if((err == 0) || (err == SOCKET_ERROR))
return false;
return FD_ISSET(connection, &set);
When I call the above function, it ALWAYS returns true. When I step through the code, err is set to ERROR_SUCCCESS and FD_ISSSET returns nonzero, even if there is no data on the socket to be read.
I am not stuck using the above solution. Any way that you can suggest to find out if the socket is ready for reading would be fine as long as it doesn not use asynchronous sockets.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Honestly, I don't really know. I'm using select to poll the socket, because if I use recv on a socket that contains no data, it will stall the entire program until data comes in. If data never comes in, then the call to recv will never return. If there's a better way to do it, then I'll be glad to hear it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER