Solved

Winsock select

Posted on 2004-08-21
9
555 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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