These server and client examples illustrate socket APIs written
for a connection-oriented protocol such as Transmission Control Protocol (TCP).
The following figure illustrates the client/server relationship
of the sockets API for a connection-oriented protocol.
Socket flow of events: Connection-oriented server
The
following sequence of the socket calls provide a description of the figure.
It also describes the relationship between the server and client application
in a connection-oriented design. Each set of flows contain links to usage
notes on specific APIs.
- The socket() function returns a socket descriptor representing
an endpoint. The statement also identifies that the INET (Internet Protocol)
address family with the TCP transport (SOCK_STREAM) is used for this socket.
- The setsockopt() function allows the local address
to be reused when the server is restarted before the required wait time expires.
- After the socket descriptor is created, the bind() function
gets a unique name for the socket. In this example, the user sets the s_addr
to zero, which allows connections to be established from any IPv4 client that
specifies port 3005.
- The listen() allows the server to accept incoming
client connections. In this example, the backlog is set to 10. This means
that the system queues 10 incoming connection requests before the system starts
rejecting the incoming requests.
- The server uses the accept() function to accept an
incoming connection request. The accept() call blocks indefinitely
waiting for the incoming connection to arrive.
- The select() function allows the process to wait for
an event to occur and to wake up the process when the event occurs. In this
example, the system notifies the process only when data is available to be
read. A 30-second timeout is used on this select() call.
- The recv() function receives data
from the client application. In this example, the client sends 250 bytes of
data. Thus the SO_RCVLOWAT socket option can be used, specifying that recv() does
not wake up until all 250 bytes of data have arrived.
- The send() function echoes the data back to the client.
- The close() function closes any open socket descriptors.
Socket flow of events: Connection-oriented client
The
Example: A connection-orientated client uses the following sequence of function
calls:
- The socket() function returns a socket descriptor representing
an endpoint. The statement also identifies that the INET (Internet Protocol)
address family with the TCP transport (SOCK_STREAM) is used for this socket.
- In the client example program, if the server string that was passed into
the inet_addr() function was not a dotted decimal IP address,
then it is assumed to be the host name of the server. In that case, use the gethostbyname() function
to retrieve the IP address of the server.
- After the socket descriptor is received, the connect() function
is used to establish a connection to the server.
- The send() function sends 250 bytes of data to the
server.
- The recv() function waits for the server
to echo the 250 bytes of data back. In this example, the server responds with
the same 250 bytes that was just sent. In the client example, the 250 bytes
of the data might arrive in separate packets, so the recv() function
can be used over and over until all 250 bytes have arrived.
- The close() function closes any open socket descriptors.