Trying to learn Non-Blocking sockets on Solaris 8, please help! ready to fork() it.

From: Chris Ritchey (rethnor_at_yahoo.com)
Date: 05/26/03


Date: 26 May 2003 12:26:39 -0700

ok I've tried for the last two days to get some good information on
how to setup a non-blocking socket. I've found limited information on
fcntl and ioctl, but no secure meat on the matter. I have tried just
opening a socket and using the ioctl(...) to set it to non-blocking
however the compiler (gcc and CC) both complain about the function
ioctl not having a prototype.

/* set to non-blocking */
int dummy = 1;
if(ioctl(listenerSD, FIONBIO, (char *) &dummy) < 0)
{
    cout << "non-blocking failed." << endl;
    return 0;
}

I am including <sys/ioctl.h> and all the necesary sockets libraries.
is there perhaps an option I need to pass to the compiler to link to a
library (-lsocket -lnfs or what ever).

Next Question would be, once(if) I've established a non-blocking
socket how do I check if there are messages/connection, not sure what
to calls them, waiting to be handled. I'm starting to think that one
would use the read function, if so how does that work, does it it
retreive this from some kind of queue? and at some point I check if
there is anything in the queue then retreive it?

I'm just really confused as far as getting the non-blocking socket set
up and how to use it, all the other posts talk about the different
variables you can pass, well I guess that is setting it up, but i'm
still confused. Also I've yet to see an example of this code in
context, so I wonder, where should this be called? if thats now
relevant how would I determine where I should call. Questions
Question! Next question, Whats the differance between fcntl and ioctl?
and what about setsocketopt function, can it be used to set the socket
to non-blocking?

Perhaps there are some very thorough tutorials that I am missing in my
google/google group searches that some one could point out to me. Or
if some one could show me how to properly set the socket up as
non-blocking and how to read from it I would be very gratefull.

I've also read forking with blocking as an option. Not sure if this is
a viable solution but would it be similiar if you forked of a seperate
process with the listener socket and each time a connection was
accepted fork it off to yet another process so that the original
process could continue listening? if thats an option what be the
advantage of forking the program over using non-blocking sockets.



Relevant Pages

  • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
    ... Yes, Blame Microsoft, but we are breaking the TCP spec, not them. ... A TCP socket isn't the same thing as a named pape or FIFO. ... Using non-blocking I/O does not mean one can suddenly shortcut the FINWAIT-1 and FINWAIT-2 states before going into TIME_WAIT, nor the 2 * MSL timeout before the TCP control block is allowed to go away. ... Otherwise, you might end up sending a RST to a dup'ed packet like a stray ACK, which seems to be almost exactly the problem at hand. ...
    (freebsd-net)
  • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
    ... Yes, Blame Microsoft, but we are breaking the TCP spec, not them. ... A TCP socket isn't the same thing as a named pape or FIFO. ... Using non-blocking I/O does not mean one can suddenly shortcut the FINWAIT-1 and FINWAIT-2 states before going into TIME_WAIT, nor the 2 * MSL timeout before the TCP control block is allowed to go away. ... Otherwise, you might end up sending a RST to a dup'ed packet like a stray ACK, which seems to be almost exactly the problem at hand. ...
    (freebsd-net)
  • Re: java socket i/o with callback/non-blocking
    ... Basically I want to have some subroutine to be called whenever there is data available to read on a socket object without having to poll it. ... Note also that the technique described is blocking, not non-blocking, as indeed is the technique you described even if the channel is in non-blocking mode. ... For just one socket, you are probably much better off to dump all the NIO stuff, and simply use blocking I/O in its own thread. ...
    (comp.lang.java.programmer)
  • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
    ... needs to set eh non-blocking socket option and use kqueue or similar ... This seems to be the correct behaviour. ... non-blocking IO in the manner it was intended. ...
    (freebsd-net)
  • non-blocking socket in Ruby 1.8.4?
    ... I'm using Ruby 1.8.4 on Linux and I'm having trouble with non-blocking ... Socket and called fcntl as above before calling accept. ...
    (comp.lang.ruby)