Mail Archives: djgpp/2000/10/02/17:15:23
Hello.
Peter Remmers wrote:
> After reading a lot of documents I know of two methods to put a
> socket into nonblocking mode:
>
> fcntl(sock, F_SETFL, O_NONBKOCK);
>
> and
>
> int on=1;
> ioctl(sock, FIONBIO, &on);
>
> What exactly is the difference between the two?
> The libsocket documentation talks about "similar":
They can both be used to set non-blocking mode, as you know. However,
fcntl()'s F_SETFL will set multiple options simultaneously. You should do
something like:
int opts;
opts = fcntl(sock, F_GETFL);
opts |= O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0) {
/* error */
}
To remove nonblocking mode, you'd use:
int opts;
opts = fcntl(sock, F_GETFL);
opts &= ~O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0) {
/* error */
}
> In what way do they differ then, if they are just *similar*?
> Don't they have the same effect?
In the libsocket the fcntl() support actually goes through the ioctl()
code. fcntl(..., O_NONBLOCK) traverses the same code path as ioctl(...,
FIONBIO, ...). So they have the same effect.
Would things be clearer if the libsocket docs used 'like' instead of
'similar'? How do you think the docs could be improved?
Hope that helps, bye,
--
Richard Dawe
[ mailto:richdawe AT bigfoot DOT com | http://www.bigfoot.com/~richdawe/ ]
- Raw text -