Now let us consider what the server process must do to accept
connections on a socket. First it must use the
to enable connection requests on the socket, and then accept each
incoming connection with a call to
accept (see Accepting Connections). Once connection requests are enabled on a server socket,
select function reports when the socket has a connection
ready to be accepted (see Waiting for I/O).
listen function is not allowed for sockets using
connectionless communication styles.
You can write a network server that does not even start running until a connection to it is requested. See Inetd Servers.
In the Internet namespace, there are no special protection mechanisms for controlling access to a port; any process on any machine can make a connection to your server. If you want to restrict access to your server, make it examine the addresses associated with connection requests or implement some other handshaking or identification protocol.
In the local namespace, the ordinary file protection bits control who has access to connect to the socket.
listenfunction enables the socket socket to accept connections, thus making it a server socket.
The argument n specifies the length of the queue for pending connections. When the queue fills, new clients attempting to connect fail with
ECONNREFUSEDuntil the server calls
acceptto accept a connection from the queue.
0on success and
-1on failure. The following
errnoerror conditions are defined for this function:
- The argument socket is not a valid file descriptor.
- The argument socket is not a socket.
- The socket socket does not support this operation.