This code example illustrates a simplified-level client API that is used in developing TI-RPC applications.
The simplified-level client API is the quickest and shortest set of code, because the client creation, control, use, and destruction are all in one call. This is convenient, but it does not allow the customization that can be done with a client handle. Defaults are accepted for timeout and buffer sizes, which is the most significant difference between the simplified level and the other levels.
#include <stdio.h> #include <errno.h> #include "myapp.h" #define EXIT 100 int main(void) { enum clnt_stat rslt; /* return value of rpc_call() */ char hostname[256]; /* buffer for remote service's hostname */ unsigned long procnum; /* procedure to call */ char filename[512]; /* buffer for filename */ char *arg = filename; /* pointer to filename buffer */ union { u_int myapp_get_uid_result; char * myapp_get_uid_string_result; int myapp_get_size_result; long myapp_get_mtime_result; char * myapp_get_mtime_string_result; u_short myapp_get_codepage_result; char * myapp_get_objtype_result; char * myapp_get_filetype_result; } result; /* a union of all the possible results */ /* get the hostname from the user */ printf("Enter the hostname where the remote service is running: \n"); scanf("%s", (char *)&hostname); myapp_print_menu(); /* print out the menu choices */ /* get the procedure number to call from the user */ printf("\nEnter a procedure number to call: \n"); scanf("%lu", &procnum); /* get the filename from the user */ printf("\nEnter a filename to stat: \n"); scanf("%s", (char *)&arg); /* switch on the input */ switch (procnum) { case NULLPROC: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, (xdrproc_t)xdr_void, (char *)NULL, /* xdr_in */ (xdrproc_t)xdr_void, (char *)NULL, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("NULLRPOC call succeeded\n"); break; case GET_UID: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_u_int, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("uid of %s: %u\n", filename, result.myapp_get_uid_result); break; case GET_UID_STRING: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_wrapstring, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("owner of %s: %s\n", filename, result.myapp_get_uid_string_result); break; case GET_SIZE: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_int, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("size of %s: %d\n", filename, result.myapp_get_size_result); break; case GET_MTIME: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_long, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("last modified time of %s: %ld\n", filename, result.myapp_get_mtime_result); break; case GET_MTIME_STRING: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_wrapstring, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("last modified time of %s: %s\n", filename, result.myapp_get_mtime_string_result); break; case GET_CODEPAGE: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_u_short, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("codepage of %s: %d\n", filename, result.myapp_get_codepage_result); break; case GET_OBJTYPE: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_wrapstring, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("object type of %s: %s\n", filename, result.myapp_get_objtype_result); break; case GET_FILETYPE: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, xdr_wrapstring, (char *)&arg, /* xdr_in */ xdr_wrapstring, (char *)&result, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("file type of %s: %s\n", filename, result.myapp_get_filetype_result); break; case END_SERVER: /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, (xdrproc_t)xdr_void, (char *)NULL, /* xdr_in */ (xdrproc_t)xdr_void, (char *)NULL, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("Service has been unregistered.\n"); printf("You must still kill the job in QBATCH\n"); break; case EXIT: /* do nothing and exit */ printf("Exiting program now.\n"); return 1; break; default: /* an invalid procedure number was entered */ /* we could just exit here */ printf("Invalid choice. Issuing NULLRPOC instead.\n"); procnum = NULLPROC; /* rpc_call(host, prognum, versnum, procnum, */ /* xdr_in, in, xdr_out, out, nettype); */ rslt = rpc_call(hostname, PROGNUM, VERSNUM, procnum, (xdrproc_t)xdr_void, (char *)NULL, /* xdr_in */ (xdrproc_t)xdr_void, (char *)NULL, /* xdr_out */ NETTYPE); /* check return value of rpc_call() */ if (rslt != RPC_SUCCESS) { fprintf(stderr, "Error calling rpc_call(%lu)\n", procnum); fprintf(stderr, "clnt_stat: %d\n", rslt); fprintf(stderr, "errno: %d\n", errno); return 1; } /* print results and exit */ printf("NULLRPOC call succeeded\n"); break; } /* end of switch(procnum) */ /* no cleanup is required for rpc_call() */ return 0; } void myapp_print_menu(void) { /* print out the procedure choices */ printf("%.2ld - GET_UID %.2ld - GET_UID_STRING\n", GET_UID, GET_UID_STRING); printf("%.2ld - GET_SIZE %.2ld - GET_MTIME\n", GET_SIZE, GET_MTIME); printf("%.2ld - GET_MTIME_STRING %.2ld - GET_CODEPAGE\n", GET_MTIME_STRING, GET_CODEPAGE); printf("%.2ld - GET_OBJTYPE %.2ld - GET_FILETYPE\n", GET_OBJTYPE, GET_FILETYPE); printf("%.2ld - END_SERVER %.2d - EXIT\n", END_SERVER, EXIT); }