Solved

Winsock select

Posted on 2004-08-21
9
560 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
[X]
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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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.

733 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