LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   what is the difference in using O_NONBLOCK in fcntl and SOCK_NONBLOCK in socket (https://www.linuxquestions.org/questions/linux-software-2/what-is-the-difference-in-using-o_nonblock-in-fcntl-and-sock_nonblock-in-socket-779678/)

jayasekar 01-04-2010 03:30 AM

what is the difference in using O_NONBLOCK in fcntl and SOCK_NONBLOCK in socket
 
Hi friends, i would like to know the difference in using O_NONBLOCK / SOCK_NONBLOCK option in two different system calls,

1. In socket system call
socket(AF_INET,SOCK_STREAM|SOCK_NONBLOCK,0);

2. In fcntl system call

if ((flags = fcntl(sock_descriptor, F_GETFL, 0)) < 0)
{
/* Handle error */
}


if (fcntl(socket_descriptor, F_SETFL, flags | O_NONBLOCK) < 0)
{
/* Handle error */
}

Is the above given two examples serves for same purpose ??
Can anyone explain it.

jjinno 01-04-2010 08:50 PM

Where did you get the reference for SOCK_NONBLOCK, and what kind of a system is this on? I have seen the fcntl() get/set many times, but that is a first on that particular socket() option.

From "man 2 socket" on Linux I see: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RAW, SOCK_RDM, SOCK_PACKET

And from the MSDN "Socket Function" for Windows I see: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET

Am I missing something?

jayasekar 01-04-2010 11:49 PM

Iam using FC-11,

man socket and you can see the below given statements.

Since Linux 2.6.27, the type argument serves a second purpose: in addi-
tion to specifying a socket type, it may include the bitwise OR of any
of the following values, to modify the behavior of socket():

SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open
file description. Using this flag saves extra calls to
fcntl(2) to achieve the same result
.

SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file
descriptor. See the description of the O_CLOEXEC flag
in open(2) for reasons why this may be useful.

I came to know that no need to use fcntl if we use sock_NONBLOCK options.


All times are GMT -5. The time now is 03:33 AM.