Solved

Winsock select

Posted on 2004-08-21
9
550 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:dirtdart
9 Comments
 
LVL 1

Accepted Solution

by:
kevinnguyen earned 125 total points
ID: 11859084
I think you should check on (connection + 1) rather than 0 in select() function:

err = select(connection + 1, &set, (fd_set*)NULL, (fd_set*)NULL, &tv);

instead

err = select(0, &set, (fd_set*)NULL, (fd_set*)NULL, &tv);

Besides, opt can be one of three values 0, 1, and 2. select() is called when there is data to read, to write and there is exception in file descriptor (connection)

opt = 0. When there is data to read
opt = 1. When there is data to write
opt = 2. When there is exception on file descriptor.

select() can return TRUE because FD_ISSET() returns nonzero when there is data to write even there is no data to read from file descriptor (connection)
0
 
LVL 5

Author Comment

by:dirtdart
ID: 11863768
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.
0
 
LVL 19

Assisted Solution

by:drichards
drichards earned 125 total points
ID: 11867614
>> I'm just using a standard socket with the non-blocking flag

Do these functions work with non-blocking sockets?  Docs are not clear on that.  With non-blocking sockets you are notified when events occur and there is not a need to poll using select.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:dirtdart
ID: 11869583
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.
0
 
LVL 3

Assisted Solution

by:daver1000
daver1000 earned 125 total points
ID: 11879722
How about setting a  small time out for the recv.
e.g
setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&chtimeout, izeof(chtimeout));


Then the recv will wait for small amount of time (not blocking).
bytesRecv = recv( ConnectSocket, recvbuf, 32, 0 );
    if ( bytesRecv==0 || bytesRecv ==WSAETIMEDOUT )
   {
     //Nothing to read
    }
0
 
LVL 1

Assisted Solution

by:rhodgson
rhodgson earned 125 total points
ID: 11911350
>>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.
>>if((err == 0) || (err == SOCKET_ERROR))  return false;

I am a little confused, ERROR_SUCCESS is a constant defined as 0, therefore if err is always set to ERROR_SUCCESS then the function should always return false.

Apart from that opt=0 should be waiting for data to be available for read, and it doesn't make any difference to select if you are using blocking or non-blocking sockets.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now