Wednesday, August 31, 2011

Applying FILE Streams to Sockets

Now it is time to introduce some source code that makes use of the concepts that have been discussed so far in my tutorials. The server program that will be presented next implements a Reverse Polish Notation (abbreviated RPN) calculator. It accepts arbitrarily long integer values, stacks them, and then permits operations to be performed upon the stacked numbers. The result of the operation is placed on the top of the stack.

The integer arithmetic will be performed by the GNU Multi-Precision (GMP) library. This library permits virtually unlimited sized integer numbers to be evaluated. Space does not permit the GMP library to be described here. The purpose of this code is simply to illustrate some server concepts using FILE streams. This same server will help illustrate some advanced topics that will be covered in the nextchapter.

Presenting the mkaddr() Function

The mkaddr.c subroutine function will be presented here, to make this project easier to read. The mkaddr() function being presented, accepts an input string that consists of an IP number and an optional port number, or a hostname and optional port number. The port number can also be a symbolic Internet service name such as "telnet" or "ftp." The function synopsis of the function is as follows


int mkaddr(void *addr,
           int *addr_len,
           char *str_addr,
           char *protocol);

The function arguments can be described as follows:
  1. The argument addr points to the receiving socket address structure. This is the socket address, which is being returned. This value must not be null.
  2. The argument addr_len is a pointer to an integer value, which will be filled with the length of the address created in addr when the function returns. The input value that is pointed to must contain the maximum size in bytes of the area pointed to by addr.
  3. Argument str_addr is the symbolic hostname and optional port number (or service). This will be more fully described later. A null pointer implies a string value of "*".
  4. The protocol argument specifies the protocol that this service will be using. A null pointer implies the protocol string "tcp".

The str_addr input string is designed to be as flexible as possible. It contains two components, separated by a colon character:


The host_name portion of the string can be one of the following:
  • An IP number such as, for example.
  • A hostname such as, for example.
  • An asterisk, which indicates that the IP address should be the value INADDR_ANY.
The colon character and the service portion of the string are optional within str_addr. When not omitted, this component can be one of the following:
  • A port number such as 8080, for example.
  • A service name, such as telnet, for example.
  • An asterisk, implying port zero. The bind(2) function call will assign a port number when this value is used.
The following examples show valid string values for the argument str_addr in the mkaddr() function call:
The mkaddr() function returns the following possible values:
  • Zero indicates that the conversion was successful.
  • -1 indicates that the host part of the string was invalid, or that the hostname was unknown.
  • -2 indicates that the port number was invalid, or that the service name was unknown.

The code for the mkaddr() subroutine is presented in Listing 10.4. This subroutine may be useful to use in projects that you might write. The instructions for compiling the code for mkaddr.c will be provided later, when the whole server is compiled.

No comments:

Post a Comment