Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    30

    Lightbulb 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.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You should be able to change the mode of the socket after it is opened with fcntl(). If you create the socket with SOCK_NONBLOCK, what does the flags returned by fcntl(sock, F_GETFL, 0) return? Is the O_NOBLOCK bit set?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    30
    Thanks for you reply Rubberman,
    if i use SOCK_NONBLOCK in socket(), and fcntl(sock, F_GETFL, 0), the O_NOBLOCK flag is set.
    man socket, you will see the below comments,
    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

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    So, it appears that option #1 is the most efficient. Of course, fcntl() will work with any file descriptor, and not just with sockets, but then you can use the O_NOBLOCK flag with open() calls as well...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...