|
|
HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 6 Using UNIX Domain Stream SocketsWriting the Server Process |
|
This section explains the calls your server process must make to connect with and serve a client process. The server process must call socket to create a communication endpoint. socket and its parameters are described in the following table.
The socket number returned is the socket descriptor for the newly created socket. This number is an HP-UX file descriptor and can be used for reading, writing or any standard file system calls after a BSD Sockets connection is established. A socket descriptor is treated like a file descriptor for an open file. After your server process has created a socket, it must call bind to bind a socket address. Until an address is bound to the server socket, other processes have no way to reference it. The server process must bind a specific pathname to this socket, which is used for listening. Otherwise, a client process would not know what pathname to connect to for the desired service. Set up the address structure with a local address before you make a bind call. bind and its parameters are described in the following table.
Once your server process has an address bound to it, it must call listen to set up a queue that accepts incoming connection requests. The server process then monitors the queue for requests (using select(2) or accept ). The server process cannot respond to a connection request until it has executed listen. listen and its parameters are described in the following table.
backlog is the preferred number of unaccepted incoming connections allowed at a given time. The actual number may be greater than the specified backlog. When the queue is full, further connection requests are rejected. A backlog of 0 specifies only 1 pending connection can exist at any given time. SOMAXCONN is defined in <sys/socket.h>. The current default setting is 20, but may change in future releases. The server process can accept any connection requests that enter its queue after it executes listen. accept creates a new socket for the connection and returns the socket descriptor for the new socket. The new socket:
accept blocks until there is a connection request from a client process in the queue, unless you are using nonblocking I/O. accept and its parameters are described in the following table.
There is no way for the server process to indicate which requests it can accept. It must accept all requests or none. |
|