user397232
user397232

Reputation: 1790

Linux socket value

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?

Upvotes: 0

Views: 453

Answers (2)

asveikau
asveikau

Reputation: 40264

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

Upvotes: 11

hager
hager

Reputation: 313

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

Upvotes: 6

Related Questions