Developing applications over TCP/IP using Internet sockets

Accepting a connection (Internet domain)

With the socket marked as listening, the server can now accept a connection:

fromlen = sizeof (from);
snew = accept(s, (struct sockaddr *) &from, &fromlen);

The server returns a new descriptor to the client on receipt of a connection (along with a new socket). If the server wishes to find out who its client is, it may supply a buffer for the client socket's address. The fromlen argument is a value-result parameter initialized by the server to indicate how much space is associated with from (the client). It is modified on return to reflect the true size of the address. Only a passive process uses accept. A passive process, such as a server process, can listen for calls and accept them, but cannot initiate them.

The call to accept will not return until a connection is available or the system call is interrupted by a signal to the process. This is called ``blocking''. The accept call cannot screen for connections from certain sources only. A user processes must provide its own screening, if required, by identifying the source of the connection and and closing down the connection if it does not wish to speak to the other process. To accept connections on more than one socket, or not block on the accept call, see ``IPC programming techniques (Internet domain)''.

Servers often bind multiple sockets. When a server accepts a connection, it usually spins off (``forks'') a process which is the connected socket. The parent then goes back to listening on the same local socket.

Next topic: Connection errors (Internet domain)
Previous topic: Using wildcards in socket addresses (Internet domain only)

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003