If the server is ready to provide its special services, it assigns one of its sockets the name (address) defined for the service concerned. In order to be able to accept the connection request of a client, the server must also execute the following two steps:
The server uses the listen() function to mark the socket for incoming connection requests as "listening". The server then monitors the socket, i.e. it waits passively for a connection request for this socket. It is now possible for any process to take up contact with the server.
listen() also causes the POSIX subsystem to place connection requests to the socket concerned in a queue. This normally prevents any connection requests being lost while the server processes another one.The server uses accept() to accept the connection request for the socket marked as "listening".
After the connection is accepted with accept(), the connection is set up between the client and server and data can be transferred.
The following program extract illustrates connection acceptance by the server in the Internet domain AF_INET:
struct sockaddr_in from; int s, newsock; ... listen(s, 5); fromlen = sizeof (from); newsock = accept(s, (struct sockaddr *)&from, &fromlen);
The following program extract illustrates connection acceptance by the server in the Internet domain AF_INET6:
struct sockaddr_in6 from; int s, newsock, fromlen; ... listen(s, 5); fromlen = sizeof(from); newsock = accept(s, (struct sockaddr_in6 *)&from, &fromlen);
The first parameter passed when listen() is called is the descriptor s of the socket over which the connection is to be set up. The second parameter defines the maximum number of connection requests which may be placed in the queue for acceptance by the server process. The POSIX subsystem currently supports a maximum of 50 pending connection requests.
The first parameter passed when accept() is called is the descriptor s of the socket over which the connection is to be set up. After accept() is executed, the from parameter contains the address of the partner application and fromlen contains the length of this address. When a connection is accepted with accept(), a descriptor is created for a new socket. This descriptor returns accept() as its result. Data can now be exchanged over the new socket. The server can accept additional connections over socket s.
An accept() call normally blocks because the accept() function does not return until a connection is accepted. When accept() is called, the server process also has no way of indicating that it only wants to accept connection requests from one or more specific partners. The server process must therefore note where the connection comes from. It must terminate the connection if it does not wish to communicate with a particular client process.
In "Advanced SOCKETS(POSIX) functions" it is described in more detail,
how a server process can accept connections on more than one socket,
how a server process can prevent the accept() call from blocking.