Example: TI-RPC intermediate-level service API

This code example illustrates an intermediate-level service API that is used in developing TI-RPC services.

The dispatch function can be reused without any changes. The only difference between the top and intermediate levels is the use of the network selection APIs. This level also creates, binds, and registers the service with the rpcbind daemon.

Note: By using the code example, you agree to the terms of the Code license and disclaimer information.
#include <stdio.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include <errno.h>
#include "myapp.h"

int main(int argc, char *argv[]) {

	struct netconfig *nconf;  /* pointer to nettype data */
	SVCXPRT *svc;             /* pointer to service handle */

	/* unregister any existing copy of this service */
	/* (void)svc_unreg(program, version) */
	svc_unreg(PROGNUM, VERSNUM);

	/* (struct netconfig *)getnetconfigent(nettype) */
	nconf = getnetconfigent(NETTYPE);
	if (nconf == (struct netconfig *)NULL) {
		fprintf(stderr, "Error calling getnetconfigent(%s)\n", NETTYPE);
		fprintf(stderr, "errno: %d\n", errno);
		return 1;
		}

	/* (SVCXPRT *)svc_tp_create(dispatch, prognum, versnum, netconf) */
	svc = svc_tp_create(myapp_dispatch, PROGNUM, VERSNUM, nconf);

        /* check for errors calling svc_tp_create() */
	if (svc == (SVCXPRT *)NULL) {
		/* print error messages and exit */
		fprintf(stderr, "Error calling %s.\n", "svc_tp_create");
		fprintf(stderr, "PROG: %lu\tVERS: %lu\tNET: %s\n", 
				PROGNUM, VERSNUM, NETTYPE);
		fprintf(stderr, "errno: %d\n", errno);
		return 1;
		}

	/* this should loop indefinitely waiting for client connections */
	svc_run();

	/* if we get here, svc_run() returned */
	fprintf(stderr, "svc_run() returned.  ERROR has occurred.\n");
	fprintf(stderr, "errno: %d\n", errno);

	/* clean up by unregistering.  then, exit */
	svc_unreg(PROGNUM, VERSNUM);

	return 1;

} /* end of main() */