Professional Web Applications Themes

select not working? - Mac Programming

I'm having some trouble using select in my program. I'm not sure if I should be asking here or in a unix newsgroup but I am on MacOS so... My code is below but for some reason select never returns anything other than 0... { // '_s' is a socket discriptor that is bound and listening. fd_set rfds; FD_ZERO( &rfds ); FD_SET( _s, &rfds ); timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; int retval = select( 1, &rfds, 0, 0, &tv ); if ( retval ) return true; else return false; } I know the socket is set up ...

  1. #1

    Default select not working?

    I'm having some trouble using select in my program. I'm not sure if I
    should be asking here or in a unix newsgroup but I am on MacOS so...

    My code is below but for some reason select never returns anything other
    than 0...


    {
    // '_s' is a socket discriptor that is bound and listening.
    fd_set rfds;
    FD_ZERO( &rfds );
    FD_SET( _s, &rfds );
    timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    int retval = select( 1, &rfds, 0, 0, &tv );
    if ( retval )
    return true;
    else
    return false;
    }

    I know the socket is set up right becuase if I make it blocking and call
    accept it works fine (but blocks of course.)


    In general the problem I'm trying to solve is how to listen on a socket
    for connection and retain the ability to shut down the server if the
    client chooses "stop" or "quit" from the menu...
    Daniel T. Guest

  2. #2

    Default Re: select not working?

    Daniel T. <postmasterearthlink.net> wrote:
    > I'm having some trouble using select in my program. I'm not sure if I
    > should be asking here or in a unix newsgroup but I am on MacOS so...
    [..]
    > int retval = select( 1, &rfds, 0, 0, &tv );
    [..]
    > In general the problem I'm trying to solve is how to listen on a socket
    > for connection and retain the ability to shut down the server if the
    > client chooses "stop" or "quit" from the menu...
    Replace &tv with NULL so it looks like this:

    int retval = select( 1, &rfds, 0, 0, NULL);

    NULL means non-blocking. The sockets have to be non-blocking, as well.

    I'm in the midst of learning sockets myself, so I can't be an expert on
    the matter. I learned the above at:

    <http://www.lowtek.com/sockets/> is a good collection of doents about
    BSD sockets, and has one in particular about select()
    (<http://www.lowtek.com/sockets/select.html>), dealing with non-blocking
    connections.

    Also, consider using CFSockets.
    Paul Mitchum Guest

  3. #3

    Default Re: select not working?

    Daniel T. wrote:
    > My code is below but for some reason select never returns anything
    > other than 0...
    The code looks correct. That's pretty much exactly what I do to test a
    socket for an incoming TCP/IP connection, and it works on MacOSX. Are
    you calling it in a loop or an event handler or something? Are you sure
    it is getting executed?

    The first parameter to select() is supposed to be FD_SETSIZE, although I
    don't know for sure if that affects your situation or not.

    You might want to set errno to zero before the call and see what it is
    afterwards. The TCP/IP stack might not agree with you that the socket
    is ready for incoming connections.
    Jhnny Fvrt (it means genetic antagonism) Guest

  4. #4

    Default Re: select not working?

    [email]usenetmile23.com[/email] (Paul Mitchum) wrote:
    > Daniel T. <postmasterearthlink.net> wrote:
    >
    > > I'm having some trouble using select in my program. I'm not sure if I
    > > should be asking here or in a unix newsgroup but I am on MacOS so...
    > [..]
    >
    > > int retval = select( 1, &rfds, 0, 0, &tv );
    >
    > [..]
    > > In general the problem I'm trying to solve is how to listen on a socket
    > > for connection and retain the ability to shut down the server if the
    > > client chooses "stop" or "quit" from the menu...
    >
    > Replace &tv with NULL so it looks like this:
    >
    > int retval = select( 1, &rfds, 0, 0, NULL);
    >
    > NULL means non-blocking. The sockets have to be non-blocking, as well.
    I think you have that backwards. According to my tests, setting the
    timer to NULL blocks (and never returns despite the fact that a client
    socket is connecting.) Either way, select never returns a positive value
    for me.
    Daniel T. Guest

  5. #5

    Default Re: select not working?

    Jhnny Fvrt (it means "genetic antagonism") <this.isfake.com> wrote:
    > Daniel T. wrote:
    > > My code is below but for some reason select never returns anything
    > > other than 0...
    >
    > The code looks correct. That's pretty much exactly what I do to test a
    > socket for an incoming TCP/IP connection, and it works on MacOSX. Are
    > you calling it in a loop or an event handler or something? Are you sure
    > it is getting executed?
    >
    > The first parameter to select() is supposed to be FD_SETSIZE, although I
    > don't know for sure if that affects your situation or not.
    >
    > You might want to set errno to zero before the call and see what it is
    > afterwards. The TCP/IP stack might not agree with you that the socket
    > is ready for incoming connections.
    Thank you for your suggestions... I did as you suggested (changed the
    first parameter to FD_SETSIZE and set and checked errno, upon return of
    select, errno is still 0. IE it still doesn't work.

    I also would like to note that my client's connect call returns with no
    error. Despite this, select never tells me the server socket has a
    listener. I must be missing something important... :-(
    Daniel T. Guest

  6. #6

    Default Re: select not working?

    Daniel T. wrote:
    > I'm having some trouble using select in my program. I'm not sure if I
    > should be asking here or in a unix newsgroup but I am on MacOS so...
    >
    > My code is below but for some reason select never returns anything other
    > than 0...
    >
    >
    > {
    > // '_s' is a socket discriptor that is bound and listening.
    > fd_set rfds;
    > FD_ZERO( &rfds );
    > FD_SET( _s, &rfds );
    > timeval tv;
    > tv.tv_sec = 0;
    > tv.tv_usec = 0;
    > int retval = select( 1, &rfds, 0, 0, &tv );
    select() checks all the file descriptors from 0 to one less than the
    value you pass as the first parameter. You are passing 1, so only the
    zero file descriptor is being checked, and so yours is not. Make it

    select(_s + 1, &rfds, 0, 0, &tv);

    Incidentally, avoid using identifiers that are prefixed with
    underscores. Most of that namespace is reserved for the implementation.

    > if ( retval )
    > return true;
    > else
    > return false;
    > }
    >
    > I know the socket is set up right becuase if I make it blocking and call
    > accept it works fine (but blocks of course.)
    >
    >
    > In general the problem I'm trying to solve is how to listen on a socket
    > for connection and retain the ability to shut down the server if the
    > client chooses "stop" or "quit" from the menu...
    Polling on select() is probably the easiest way. An alternative way is
    to use blocking sockets in a separate thread and send a signal (e.g. via
    alarm()) when you want to cancel. Signals cause most blocking functions
    to return immediately.

    -Peter

    Peter Ammon Guest

  7. #7

    Default Re: select not working?

    In article <postmaster-2D22F1.00195018072003nnrp01.earthlink.net>,
    "Daniel T." <postmasterearthlink.net> wrote:
    > Jhnny Fvrt (it means "genetic antagonism") <this.isfake.com> wrote:
    >
    > > Daniel T. wrote:
    > > > My code is below but for some reason select never returns anything
    > > > other than 0...
    > >
    > > The code looks correct. That's pretty much exactly what I do to test a
    > > socket for an incoming TCP/IP connection, and it works on MacOSX. Are
    > > you calling it in a loop or an event handler or something? Are you sure
    > > it is getting executed?
    > >
    > > The first parameter to select() is supposed to be FD_SETSIZE, although I
    > > don't know for sure if that affects your situation or not.
    > >
    > > You might want to set errno to zero before the call and see what it is
    > > afterwards. The TCP/IP stack might not agree with you that the socket
    > > is ready for incoming connections.
    >
    > Thank you for your suggestions... I did as you suggested (changed the
    > first parameter to FD_SETSIZE and set and checked errno, upon return of
    > select, errno is still 0. IE it still doesn't work.
    >
    > I also would like to note that my client's connect call returns with no
    > error. Despite this, select never tells me the server socket has a
    > listener. I must be missing something important... :-(
    I have it working now! Straingest thing, I simply created a new project
    and brought in the exact same source code, and compiled it. It ran
    correctly without a hitch. Odd...
    Daniel T. Guest

  8. #8

    Default Re: select not working?

    Peter Ammon wrote:
    > select() checks all the file descriptors from 0 to one less than the
    > value you pass as the first parameter.
    I don't think that's correct. select() is supposed to check however
    many sockets you add to your fd_set, and the first parameter gives the
    maximum size of the set.
    Jhnny Fvrt (it means genetic antagonism) Guest

  9. #9

    Default Re: select not working?

    Daniel T. wrote:
    > In article <postmaster-2D22F1.00195018072003nnrp01.earthlink.net>,
    > "Daniel T." <postmasterearthlink.net> wrote:
    >
    >
    >>Jhnny Fvrt (it means "genetic antagonism") <this.isfake.com> wrote:
    >>
    >>
    >>>Daniel T. wrote:
    >>>
    >>>>My code is below but for some reason select never returns anything
    >>>>other than 0...
    >>>
    >>>The code looks correct. That's pretty much exactly what I do to test a
    >>>socket for an incoming TCP/IP connection, and it works on MacOSX. Are
    >>>you calling it in a loop or an event handler or something? Are you sure
    >>>it is getting executed?
    >>>
    >>>The first parameter to select() is supposed to be FD_SETSIZE, although I
    >>>don't know for sure if that affects your situation or not.
    >>>
    >>>You might want to set errno to zero before the call and see what it is
    >>>afterwards. The TCP/IP stack might not agree with you that the socket
    >>>is ready for incoming connections.
    >>
    >>Thank you for your suggestions... I did as you suggested (changed the
    >>first parameter to FD_SETSIZE and set and checked errno, upon return of
    >>select, errno is still 0. IE it still doesn't work.
    >>
    >>I also would like to note that my client's connect call returns with no
    >>error. Despite this, select never tells me the server socket has a
    >>listener. I must be missing something important... :-(
    >
    >
    > I have it working now! Straingest thing, I simply created a new project
    > and brought in the exact same source code, and compiled it. It ran
    > correctly without a hitch. Odd...
    I implore you not to use that code, since it really is incorrect. The
    doentation for select() could be worded better, and the interface
    could be a lot better, but select() really does check all of the
    descriptors from 1 to one less than the first parameter. See, for
    example, this code:

    peterammon: ~ ) cat test.c
    #include <sys/types.h>
    #include <sys/time.h>
    #include <unistd.h>

    int main(void) {
    int fd=1; /* stdout */
    fd_set rfds;
    struct timeval tv;

    /* check select, passing it 1 */
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tv.tv_sec=0;
    tv.tv_usec=0;
    int first=select(1, 0, &rfds, 0, &tv);

    /* check select, passing it fd + 1 */
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tv.tv_sec=0;
    tv.tv_usec=0;
    int second=select(fd+1, 0, &rfds, 0, &tv);

    printf("select(1...) returned %d\n", first);
    printf("select(fd+1) returned %d\n", second);
    return 0;
    }
    peterammon: ~ ) cc test.c; ./a.out
    select(1...) returned 0
    select(fd+1) returned 1

    Or see lots of examples on the web:

    [url]http://www.lowtek.com/sockets/select.html[/url]
    [url]http://cscene.unitycode.org/topics/unix/cs2-13.xml.html[/url]

    Both of those pass one more than the highest file descriptor they want
    to select() on.

    -Peter

    Peter Ammon Guest

  10. #10

    Default Re: select not working?

    Peter Ammon wrote:
    > Daniel T. wrote:
    >
    >> In article <postmaster-2D22F1.00195018072003nnrp01.earthlink.net>,
    >> "Daniel T." <postmasterearthlink.net> wrote:
    >>
    >>
    >>> Jhnny Fvrt (it means "genetic antagonism") <this.isfake.com>
    >>> wrote:
    >>>
    >>>
    >>>> Daniel T. wrote:
    >>>>
    >>>>> My code is below but for some reason select never returns anything
    >>>>> other than 0...
    >>>>
    >>>>
    >>>> The code looks correct. That's pretty much exactly what I do to test a
    >>>> socket for an incoming TCP/IP connection, and it works on MacOSX. Are
    >>>> you calling it in a loop or an event handler or something? Are you
    >>>> sure
    >>>> it is getting executed?
    >>>>
    >>>> The first parameter to select() is supposed to be FD_SETSIZE,
    >>>> although I
    >>>> don't know for sure if that affects your situation or not.
    >>>>
    >>>> You might want to set errno to zero before the call and see what it is
    >>>> afterwards. The TCP/IP stack might not agree with you that the socket
    >>>> is ready for incoming connections.
    >>>
    >>>
    >>> Thank you for your suggestions... I did as you suggested (changed the
    >>> first parameter to FD_SETSIZE and set and checked errno, upon return
    >>> of select, errno is still 0. IE it still doesn't work.
    >>>
    >>> I also would like to note that my client's connect call returns with
    >>> no error. Despite this, select never tells me the server socket has a
    >>> listener. I must be missing something important... :-(
    >>
    >>
    >>
    >> I have it working now! Straingest thing, I simply created a new
    >> project and brought in the exact same source code, and compiled it. It
    >> ran correctly without a hitch. Odd...
    >
    >
    > I implore you not to use that code...
    Sorry, I didn't realize that you changed the first parameter to
    FD_SETSIZE. FD_SETSIZE is going to be larger than the file descriptor
    of the socket, so doing that is fine and your code is safe.

    -Peter

    Peter Ammon Guest

Similar Threads

  1. SELECT WHERE NOT, is not working
    By MargaretGillon@chromalloy.com in forum PostgreSQL / PGSQL
    Replies: 6
    Last Post: January 5th, 10:59 PM
  2. Replies: 0
    Last Post: September 24th, 03:24 AM
  3. Select Max not working here
    By J. Muenchbourg in forum ASP
    Replies: 3
    Last Post: September 23rd, 07:26 PM
  4. Replies: 0
    Last Post: September 11th, 11:26 AM
  5. Replies: 0
    Last Post: September 11th, 12:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139