Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Winsock select

Posted on 2004-08-21
9
557 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 196
Unable to start eclipse ? 17 153
How to print into std::string like sprintf using C++11? 12 81
Beginner to Unreal Engine 4 5 94
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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