TCP/IP Programming Manual

Server Program
The basic steps performed by a server program are:
1. Designate the NonStop TCP/IP, TCPSAM, or TCP6SAM process name (optional).
2. Create a socket.
3. Bind the socket to a well-known port (required for most servers; does not apply to RAW;
optional for servers started by the LISTNER process).
4. Listen for connections (required for TCP; not done for UDP or RAW).
5. Accept incoming connections. When a connection is received, create a new socket and accept
the connection on the new socket (required for TCP; optional for UDP; not done for RAW).
6. Start data transfer (if step 5 was done, use the new socket created in that step).
7. Shut down the socket (optional for TCP; not done for UDP or RAW).
8. Close the socket.
For servers, some of the calls or call requirements vary depending on the way the server operates.
Servers that operate at a well-known port (one that is associated with a specific service provided
by the server) must perform a call to bind to permanently associate the socket with that port.
Steps 1 through 3 and 6 through 8 are used in the same way by servers and clients. See TCP
Client and Server Programs (page 39) for descriptions of the similar steps. The steps for listening
for and accepting connections apply only to servers; these steps are described below.
Listening for Connections
The listen routine is provided in the 4.3 BSD UNIX operating system to set the queue length for
pending TCP connections on a socket. The NonStop TCP/IP process or Parallel Library TCP/IP, or
NonStop TCP/IPv6 subystem sets a default value of 5 for the queue length. Using the listen
routine, you can set the queue length to a value from 1 through 5; TCP servers must call listen
before accepting a connection.
Accepting a Connection
A server typically uses one socket to check for connections and another socket to transfer data (if
the same process performs both functions). This technique allows the server to check for a new
connection on the first socket, accept the new connection, and start data transfer on a second
socket. The server can then check for another new connection on the first socket without waiting
for the data transfer to complete. The accept routine permits this type of operation.
The accept routine performs three steps. First, the routine checks for connections on an existing
socket. Then, when a connection request arrives, accept creates a new socket for the data transfer.
Finally, it accepts the connection on the new socket. For nowait operations, a program must issue
a sequence of these calls to perform these functions:
accept_nw
AWAITIOX
socket_nw
AWAITIOX
accept_nw2
AWAITIOX
Server Programs Started by LISTNER
The LISTNER process described in LISTNER Process (page 31), checks for connections. When
LISTNER receives a connection request, it starts another process and passes the connection
information to that process, which in turn handles the data transfer. The LISTNER process calls
accept_nw. After the AWAITIOX command completes, LISTNER passes the returned remote
address and port number to the second process.
If you are programming a server that you want LISTNER to start, your server program must call
socket to create a socket, call bind to bind the socket to a local address and port, and then call
accept_nw2 to accept the connection for data transfer (passing to accept_nw2 the socket
Basic Steps for Programs 37