• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Servers started by inetd.

If you have a service defined in /etc/services, and inetd fires up your server when a requested arrives on your port, file descriptors 0, 1 and 2 are available to the server process. As sockets are bidirectional, which ones do we use for send/recv? Should I be using 0 for recv and 1 for send? I have tried using only 0 and it works fine. But why have 0,1 and 2 as socket descriptors as we need only one socket descriptor for operations.

Please e-mail me at vividh@hotmail.com

Thanks in advance.

Vividh
0
vividh
Asked:
vividh
1 Solution
 
ahoffmannCommented:
0 is STDIN, 1 is STDOUT and 2 is STDERR
This is a standard, somehow, in UNIX ;-)
0
 
seedyCommented:
The inet daemon accepts the connection(if it is stream socket), forks and the child process handles the service request.  The child closes all file descriptors other than the accepted socket descriptor, calls dup2 to duplicate the accepted socket descriptor on to file descriptors 0,1 and 2.  The original socket descriptor is also then closed.  Doing this the only file descriptors that are open in the child are 0,1 and 2.  It does few other stuff related to security, and then does an exec to the execute the appropriate server-program to handle the request.

As said above 0,1 and 2 are the same physical socket connection.  I guess the reason may be to provide flexibility in writing your server.  You do not have to use 0 for recv and 1 for send; though it may not hurt either.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now