I have Mac OS X 10.2.1.
I use sockets and I want to use 'select()' to see when data is available on
the socket. This is what I've made:
int serversock = socket(AF_INET, SOCK_STREAM, 0);
int optval = 1;
setsockopt(serversock , SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
...
bind(serversock , ...);
listen(serversock , 5);
...
int sock = accept(serversock, ...);
// set non-blocking
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | FNDELAY);
...
fd_set ready;
FD_ZERO(&ready);
FD_SET(sock, &ready);
select(1, &ready, NULL, NULL, NULL);
read(sock, buf, 1024);
...
The problem is that my thread is always blocked on the 'select()' although
data is available. It seems that 'select()' doesn't see that data is
available on my socket. Has anyone an idea why ?
Thanks.
Cédric Pillonel
Hm, seems odd.
On the other hand, you use non-blocking. You could try and use 'int n;
ioctl(sock,FIONREAD,&n);' to see how many bytes are waiting.
Ofcourse that isn't the best in Unix terms, but may help until a real
answer arrives.
Ruud van Gaal
Free car sim: http://www.racer.nl/
Pencil art : http://www.marketgraph.nl/gallery/
I encountered this behavior as well, and resolved it by providing a non-NULL
timeout value with zero field values.
Cary
Rich
>read(sock, buf, 1024);
>...
>
>The problem is that my thread is always blocked on the 'select()' although
>data is available. It seems that 'select()' doesn't see that data is
>available on my socket. Has anyone an idea why ?
>
>Thanks.
>
>Cédric Pillonel
>
>
>
--
--------------------------------------------------------------------
Rich Seibel, Software Engineer (314)579-0066 ext 211
Object Computing, Inc. seib...@ociweb.com
Need ACE training? See http://www.theaceorb.com
--------------------------------------------------------------------
Try this:
select(sock+1, &ready, NULL, NULL, NULL);
The first argument to select is the number of *bits* in the fd_set to
scan, not the number of *non-zero bits* in the fd_set. Passing nfds=1
means select() will only check the first bit in the fd_set, which
corresponds to descriptor 0. In your case, sock's descriptor is probably
not zero (because descriptors zero, one, and two are usually stdin, stdout,
and stderr), so select() never actually checks for data on your socket.
Passing sock+1 means select() will use bits [0..sock] in the fd_set,
and your socket should be handled correctly.
See `man 2 select` for details.
--
Greg Parker gpa...@apple.com Java & Objective-C
The other were right ofcourse; use select(maxFD+1,...).
About the above comment: that changes the whole scheme: with
select(...,timeout=NULL) you wait forever until some fd gets a signal,
but by adding a timeout struct with zero fields you get a select()
that returns immediately. Which is like busy polling.
This is good information, thanks. The network code is always busy (the game
is an online game) and the polling behavior must be masked by quantity of
data. I'll change that, it should yield a bit of a performance boost.
Cary
Perhaps a sleep()-type function can also help (if most of the time
there's network bytes waiting, the select() might not really do you
too much good). Linux has nanosleep(); I don't know about the Mac.
Also, I have at some point on IRIX (SGI) seen a function that only
triggers an FD if a specified NUMBER of bytes are available. So you
can wait until at least a bunch of bytes is available.
On IRIX, it is used in audio programming to notify you when the audio
buffer becomes empty enough (with audio you don't want to be notified
when just 1 byte is free, you want to wait until at least 0.1 sec or
so of audio becomes available before you pump in a new batch of
samples).
I don't know however how they pulled off the trick. Probably an
ioctl() that may not be supported on every fd.
> Perhaps a sleep()-type function can also help (if most of the time
> there's network bytes waiting, the select() might not really do you
> too much good). Linux has nanosleep(); I don't know about the Mac.
nanosleep() is supported in 10.1, but there's no prototype for it. If
you add the prototype, it'll work. It's in the headers in 10.2.
-Eric
--
Eric Albert ejal...@stanford.edu
http://rescomp.stanford.edu/~ejalbert/
and then:
rc = select(h + 1, ...);
"Cédric Pillonel" <cedric....@swisscom.com> schreef in bericht
news:1035812880.581025@exnews...