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 ...
- 11-27-2008 #1
About socket value
I just wondering, does socket value always begin from 3 in every Linux distros? You see.. when I type
the value of sock will be 3. Is there any possibility the very first socket value is other than 3?Code:sock= socket(AF_INET, SOCK_STREAM, 0);
- 11-27-2008 #2
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.
- 11-27-2008 #3No.does socket value always begin from 3
It doesn't matter which Linux distro you use.does socket value always begin from 3 in every Linux distros?
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.
Here's the output I 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
You may well ask, "Why would a user ever want to do that?"Code:3 4
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:
Evil. Nuff said.vvv evil vvv
Why would a user ever want to do that?
^^^ evil ^^^--
Bill
Old age and treachery will overcome youth and skill.
- 11-28-2008 #4
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?
- 11-28-2008 #5
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
So if you open any additional files in the program, you'll start at 4 in this case.Code:s 3>/dev/null
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.
- 12-03-2008 #6


Reply With Quote