Link to home
Start Free TrialLog in
Avatar of dirtdart
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.
ASKER CERTIFIED SOLUTION
Avatar of kevinnguyen
kevinnguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dirtdart
dirtdart

ASKER

Thank you for your comment.  Let me clarify a bit, though.  I am writing this exclusively for Windows sockets.  According to the documentation, the nfds (first parameter of select) is ignored and only included for compaitiblity with Berkley sockets.  So I don't think that is the problem.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial