Find the answer to your Linux question:
Results 1 to 6 of 6
I just wondering, does socket value always begin from 3 in every Linux distros? You see.. when I type Code: sock= socket(AF_INET, SOCK_STREAM, 0); the value of sock will be ...
  1. #1
    Just Joined! garry_3peace's Avatar
    Join Date
    Oct 2008
    Posts
    67

    About socket value

    I just wondering, does socket value always begin from 3 in every Linux distros? You see.. when I type

    Code:
    sock= socket(AF_INET, SOCK_STREAM, 0);
    the value of sock will be 3. Is there any possibility the very first socket value is other than 3?

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    A possible explanation could be that stdin=0, stdout=1 and stderr=2 on any POSIX system, but I wouldn't count on it.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    does socket value always begin from 3
    No.
    does socket value always begin from 3 in every Linux distros?
    It doesn't matter which Linux distro you use.

    I would bet the farm that the behavior is the same in every Linux distro except perhaps ones with unusual execution environments such as embedded systems. And it may well be the same with other POSIX systems as well.

    But no matter which Linux system you use, you can make the value of the first newly opened file descriptor be something other than 3. Run the following bash script on any ordinary Linux distribution and see what output you get.
    Code:
    #!/bin/bash
    cat>s.c<<X;cc -Wall s.c -o s; s; s 3>/dev/null
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    int main(void)
    { printf("%d\n",socket(AF_INET,SOCK_STREAM,0)); return 0;}
    X
    Here's the output I get:
    Code:
    3
    4
    You may well ask, "Why would a user ever want to do that?"

    One of the easiest ways to infuriate a user of your software, or simply to break your software, is to make any design decision based on this question:
    vvv evil vvv

    Why would a user ever want to do that?

    ^^^ evil ^^^
    Evil. Nuff said.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined! garry_3peace's Avatar
    Join Date
    Oct 2008
    Posts
    67
    Geez~

    Btw, what is the meaning of the bash script. Too complicated for me to comprehend...

    Is it redirecting the code to s.c and then compile it using cc ? Later dump the third file descriptor to /dev/null?

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    When you do a socket(), an open(), or even an fopen(), that "uses up" a file descriptor. As GNU-Fan noted, file descriptors start with 0 (for standard input), 1 (for standard output), and 2 (for standard error). You can instruct bash to run a program with additional files open, where you indicate the file descriptors, as in
    Code:
    s 3>/dev/null
    So if you open any additional files in the program, you'll start at 4 in this case.

    If you close a file descriptor, that one becomes available again for later use in your program. Each time, the system will go for the lowest-numbered available file descriptor.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined! garry_3peace's Avatar
    Join Date
    Oct 2008
    Posts
    67
    Ow I see.... I get it now.... thanks for the explanation

Posting Permissions

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