ITEM: D2848L

Question about sockets


Question:

Need to do non-blocking reads against a socket.  How is this
performed?

Answer:
  There are actually several ways that this can be accomplished.
One way (probably the easiest and most flexible) is to use the
select() call prior to the read.  With select(), your program would
request notification when something was available to read.  You could
specify for the select to just check (and return immediately) by
specifying a timeout=0. Or you could specify some time limit (in
seconds and microsecs) so that if there was nothing new within the
time limit, select() would return indicating nothing is available.  If
some thing is received prior to the timeout, select() returns
immediately indicating something is available and your program would
then issue the read.  Or, your program could pass a null pointer as
the timelimit and this tells select to hang until something is
available ('forever').  The select can be used before the read(),
recv() or recvmsg() subroutines.

Alternate methods are to use ioctl() to set the FIONBIO or fcntl() to
set O_NONBLOCK so that calls to read will not block (and you would not
have to use the select() call).  You will need to link your program
with the BSD library (cc -lbsd...) to use the ioctl(FIONBIO) method.


Support Line: Question about sockets ITEM: D2848L
Dated: October 1993 Category: N/A
This HTML file was generated 99/06/24~13:30:57
Comments or suggestions? Contact us