tags:

views:

86

answers:

2

I have a client-server program, on the server side:

sockListen = socket(PF_INET, SOCK_STREAM, 0);

socketListen always seems to equal 3. Why? how about 0, 1 or 2? What is the value range of sockets in Linux?

+5  A: 

The return value is a file descriptor. File descriptors 0, 1 and 2 are already open. (stdin, stdout and stderr, respectively)

hager
+10  A: 

0, 1, and 2 are stdin, stdout, and stderr, respectively. 3 is the next available one. If you created another, or opened a file, etc., it'd get 4. And so and and so forth.

But you shouldn't rely on this at all. Code your application to treat the integers as opaque objects. Only compare them to values to see if they're negative (indicating an error).

asveikau