[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Communications Programming Concepts

Using XDR Example

Assume that a person's gross assets and liabilities are to be exchanged among processes. Also, assume that these values are important enough to warrant their own data type:

struct gnumbers {
   long g_assets;
   long g_liabilities;
};

The corresponding eXternal Data Representaton (XDR) routine describing this structure would be:

bool_t      /*  TRUE is success, FALSE is failure  */
xdr_gnumbers(xdrs, gp)
   XDR *xdrs;
   struct gnumbers *gp;
{
   if (xdr_long(xdrs, &gp->g_assets) &&
      xdr_long(xdrs, &gp->g_liabilities))
      return(TRUE);
   return(FALSE);
}

The xdrs parameter is neither inspected nor modified before being passed to the subcomponent routines. However, programs should always inspect the return value of each XDR routine call, and immediately give up and return False if the subroutine fails.

This example also shows that the bool_t type is declared as an integer whose only values are TRUE (1) and FALSE (0). This document uses the following definitions:

#define bool_t     int
#define TRUE       1
#define FALSE      0

Keeping these conventions in mind, the xdr_gnumbers routine can be rewritten as follows:

xdr_gnumbers(xdrs, gp)
   XDR *xdrs;
   struct gnumbers *gp;
{
   return(xdr_long(xdrs, &gp->g_assets) &&
      xdr_long(xdrs, &gp->g_liabilities));
}

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]