Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
D.  RPC Code Examples Print Message Program With Remote Version  Previous   Contents   Next 
   
 

Batched Code Example


Example D-11 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 D-12 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);
}

Non-Batched Example

This example is included for reference only. It is a version of the batched client string rendering service, written as a non-batched program.


Example D-13 Unbatched Version of Batched Client

#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);
	}
	total_timeout.tv_sec = 20;
	total_timeout.tv_usec = 0;
	while (scanf("%s", s) != EOF) {
		if(clnt_call(client, RENDERSTRING, xdr_wrapstring, &s,
		    xdr_void, (caddr_t) NULL, total_timeout) != RPC_SUCCESS) {
			clnt_perror(client, "rpc");
			exit(1);
		}
	}
	clnt_destroy(client);
	exit(0);}

 
 
 
  Previous   Contents   Next