Solved

Winsock select

Posted on 2004-08-21
9
548 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:dirtdart
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

17 Experts available now in Live!

Get 1:1 Help Now