Previous Page
Next Page

A.3 Library Code

Some of the examples in the text used library functions from ETCP. The library code is publicly available, so rather than complete listings, short descriptions of the functions are given.

The error Function

The first function, error, is a generalized diagnostic routine. It can report errors or diagnostics and continue processing, or it can report a fatal error and terminate the process. The function writes its output to STDERR. The error prototype is:

If status is 0, error will output the diagnostic and return. When status is nonzero, error will output the diagnostic and exit with a status of status. If err is nonzero, error treats it as an errno value and will append the error string associated with that value by calling strerror. The format parameter is a standard printf formatting string that error uses along with any following arguments to format the diagnostic.

The tcp_server and tcp_client Functions

The tcp_server and tcp_client functions establish a TCP connection. In the case of tcp_server, the function accepts a host and a port and returns a listening socket. In the case of tcp_client, the function also accepts a host and a port, and returns a socket connected with the specified peer.

The prototype for tcp_server is:

The host parameter points to a string that tells tcp_server which interface it should listen on. The string can be an ASCII IP address, a host name, or NULL. If it is NULL, tcp_server will listen on all the host's interfaces. Similarly, the port parameter points to a string that tells tcp_server which port to listen on. It can be either an ASCII port number or a symbolic service name. The function returns a socket that is listening for connections on the specified interface and port. The caller is expected to use this socket as an input to the accept function.

The prototype for tcp_client is:

The host parameter points to a string that contains the name or address of the host to connect to. It can be either an ASCII IP address or the name of the host. The port parameter points to a string that contains the port number or name that tcp_client should connect to. It can be either an ASCII port number or a symbolic service name. The function returns a connected socket or terminates.


Previous Page
Next Page