Go to main content

ONC+ RPC Developer's Guide

Exit Print View

Updated: March 2019
 
 

Batched Code Example

Example 109  Batched Client Program
#include <stdio.h>
#include <rpc/rpc.h>
#include "windows.h"

main(argc, argv)
	int             argc;
	char          **argv;
{
	struct timeval  total_timeout;
	register CLIENT *client;
	enum clnt_stat  clnt_stat;
	char            buf[1000], *s = buf;

	if ((client = clnt_create(argv[1], WINDOWPROG, WINDOWVERS,
	              "CIRCUIT_V")) == (CLIENT *) NULL) {
		clnt_pcreateerror("clnt_create");
		exit(1);
	}

	timerclear(&total_timeout);
	while (scanf("%s", s) != EOF) {
		clnt_call(client, RENDERSTRING_BATCHED, xdr_wrapstring,
		  &s,xdr_void, (caddr_t) NULL, total_timeout);
	}

	/* Now flush the pipeline */
	total_timeout.tv_sec = 20;
	clnt_stat = clnt_call(client, NULLPROC, xdr_void,
			 (caddr_t) NULL, xdr_void, (caddr_t) NULL,
                         total_timeout);
	if (clnt_stat != RPC_SUCCESS) {
		clnt_perror(client, "rpc");
		exit(1);
	}
	clnt_destroy(client);
	exit(0);
}
Example 110  Batched Server Program
#include <stdio.h>
#include <rpc/rpc.h>
#include "windows.h"

void            windowdispatch();
main()
{
	int num;

	num = svc_create(windowdispatch, WINDOWPROG, WINDOWVERS,
	      "CIRCUIT_V");
	if (num == 0) {
		fprintf(stderr, "can't create an RPC server\n");
		exit(1);
	}
	svc_run();                       /* Never returns */
	fprintf(stderr, "should never reach this point\n");
}

void
windowdispatch(rqstp, transp)
	struct svc_req *rqstp;
	SVCXPRT        *transp;
{
	char           *s = NULL;

	switch (rqstp->rq_proc) {
		case NULLPROC:
			if (!svc_sendreply(transp, xdr_void, 0))
				fprintf(stderr, "can't reply to RPC call\n");
			return;
		case RENDERSTRING:
			if (!svc_getargs(transp, xdr_wrapstring, &s)) {
				fprintf(stderr, "can't decode arguments\n");
				/* Tell caller an error occurred */
				svcerr_decode(transp);
				break;
			}
			/* Code here to render the string s */
			if (!svc_sendreply(transp, xdr_void, (caddr_t) NULL))
				fprintf(stderr, "can't reply to RPC call\n");
			break;
		case RENDERSTRING_BATCHED:
			if (!svc_getargs(transp, xdr_wrapstring, &s)) {
				fprintf(stderr, "can't decode arguments\n");
				/* Be silent in the face of protocol errors */
				break;
			}
			/* Code here to render string s, but send no reply! */
			break;
		default:
			svcerr_noproc(transp);
			return;
	}
	/* Now free string allocated while decoding arguments */
	svc_freeargs(transp, xdr_wrapstring, &s);
}