The Lower Layer Server



#include "str.h"

void main()
{
	SVCXPRT *transp;
	int addstr();
	int addnums();
	
	transp = svcudp_create(RPC_ANYSOCK);
	if  (transp == NULL)      {
	     fprintf(stderr, "can't create an RPC server. try using the magic word.\n");
	     return;
	}
	pmap_unset(PROGRAM, VERSION);
	pmap_unset(PROGRAM2, VERSION);
	if (!svc_register(transp, PROGRAM, VERSION, addstr, IPPROTO_UDP))   {
	     fprintf(stderr, "can't register addstr service.  wrong forms.\n");
	     return;
	}
	if (!svc_register(transp, PROGRAM2, VERSION, addnums, IPPROTO_UDP))   {
	     fprintf(stderr, "can't register addnums service. I'm an illegal immigrant.\n");
	     return;
	}

	fprintf(stderr, "server is running.....");
	svc_run();
	fprintf(stderr, "error!  svc_run returned!");
	return;
}

addstr(rqstp, transp)
	struct svc_req *rqstp;
	SVCXPRT *transp;
{
	char num[MAXLEN];
	int sum, nums, i, intRead;
	char *ptr;

	switch (rqstp->rq_proc)     {
	case NULLPROC:
	     if (!svc_sendreply(transp, xdr_void, 0))
		fprintf(stderr, "can't reply to RPC call. not enough change.\n");
	     return;
	case RUSERSPROC_NUM:   {
	     if (!svc_getargs(transp, xdr_str, num))   {
		svcerr_decode(transp);
		fprintf(stderr, "couldn't get number string. couldn't get Betsy's number either. ho-hum...\n");
		return;
	     }
	     fprintf(stderr, "..");
	     nums = 0;
	     for (i = 0; i < strlen(num); i++)    {
		if (num[i] == ' ')		
		nums++;
	     }
	     nums++;  /* counts the number of ints  */

	     ptr = num;
	     sum = 0;
	     intRead = 0;
	     for (i = 0; i < nums; i++)    {
		if (i == (nums - 1))
		    sscanf(ptr, "%d", &intRead);
		else    {
		    sscanf(ptr, "%d ", &intRead);
		    while (*ptr != ' ')    {
			ptr++;     }
		    ptr++;    }
		sum += intRead;   }
		if (!svc_sendreply(transp, xdr_int, &sum))    {
		     fprintf(stderr, "can't reply to RPC call\n");
	             return;     }
	     }
	     return;
	default:
	     svcerr_noproc(transp);
	     return;
	}
}

addnums(rqstp, transp)
	struct svc_req *rqstp;
	SVCXPRT *transp;
{
	int num[MAXLEN];
	int sum, i;

	switch (rqstp->rq_proc)     {
	case NULLPROC:
	     if (!svc_sendreply(transp, xdr_void, 0))
		fprintf(stderr, "can't reply to RPC call. not able to take collect calls.\n");
	     return;
	case RUSERSPROC_NUM:   {
	     if (!svc_getargs(transp, xdr_arry, &num))   {
		svcerr_decode(transp);
		fprintf(stderr, "couldn't get numbers. try a magic wand.");
		return;
		}
	     fprintf(stderr, "..");
	     sum = 0;
	     for (i = 1; i < num[0]; i++)    {
 		sum += num[i];   
	     }   
	     if (!svc_sendreply(transp, xdr_int, &sum))    {
		fprintf(stderr, "can't reply to RPC call. sorry.\n");
	        return;     }   
	}
	     return;
	default:
	     svcerr_noproc(transp);
	     return;
	}
}

[Project Page] [Programs Page] [REU Page] [Scientific Visualization]

©1998 MRC