These examples enable a server to communicate with a client by
using the send_file() and accept_and_recv() APIs.
Socket flow of events: Server send contents of a file
The
following sequence of the socket calls provides a description of the graphic.
It also describes the relationship between two applications that send and
receive files. The first example uses the following sequence of function calls:
- The server calls socket(), bind(),
and listen() to create a listening socket.
- The server initializes the local and remote address structures.
- The server calls accept_and_recv() to wait for an incoming
connection and to wait for the first data buffer to arrive over this connection.
This call returns the number of bytes that is received and the local and remote
addresses that are associated with this connection. This call is a combination
of the accept(), getsockname(), and recv() APIs.
- The server calls open() to open the file whose name
was obtained as data on the accept_and_recv() from the
client application.
- The memset() function is used to set all of the fields
of the sf_parms structure to an initial value of 0. The server sets the file
descriptor field to the value that open() returned. The
server then sets the file bytes field to -1 to indicate that the server should
send the entire file. The system is sending the entire file, so you do not
need to assign the file offset field.
- The server calls send_file() to transmit
the contents of the file. send_file() does not complete
until the entire file has been sent or an interruption occurs. The send_file() function
is more efficient because the application does not need to go
into a read() and send() loop until
the file finishes.
- The server specifies the SF_CLOSE flag on the send_file() API.
The SF_CLOSE flag informs the send_file() API
that it should automatically close the socket connection when the last byte
of the file and the trailer buffer (if specified) have been sent successfully.
The application does not need to call close() if the SF_CLOSE flag
is specified.
Socket flow of events: Client request for file
The
second example uses the following sequence of function calls:
- This client program takes from zero to two parameters.
The first parameter
(if specified) is the dotted-decimal IP address or the host name where the
server application is located.
The second parameter (if specified) is
the name of the file that the client attempts to obtain from the server. A
server application sends the contents of the specified file to the client.
If the user does not specify any parameters, then the client uses INADDR_ANY
for the server's IP address. If the user does not specify a second parameter,
the program prompts the user to enter a file name.
- The client calls socket() to create a socket descriptor.
- The client calls connect() to establish a connection
to the server. Step one obtained the IP address of the server.
- The client calls send() to inform the server what file
name it wants to obtain. Step one obtained the name of the file.
- The client goes into a "do" loop calling recv() until
the end of the file is reached. A return code of 0 on the recv() means
that the server closed the connection.
- The client calls close() to close the socket.