rpc编程(simple)

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "math.h"

struct MATH *
math_proc_2_svc(struct MATH *argp, struct svc_req *rqstp)
{
	static struct MATH  result;
	switch(argp->op){
		case ADD:
			result.result = argp->arg1 + argp->arg2;
			break;
		case SUB:
			result.result = argp->arg1 - argp->arg2;
			break;
		case MUL:
			result.result = argp->arg1*argp->arg2;
			break;
		case DIV:
			result.result =argp->arg1/argp->arg2;
			break;
		default:
			break;
	}
	/*
	 * insert server code here
	 */

	return &result;
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "math.h"

struct MATH *
math_proc_2_svc(struct MATH *argp, struct svc_req *rqstp)
{
	static struct MATH  result;
	switch(argp->op){
		case ADD:
			result.result = argp->arg1 + argp->arg2;
			break;
		case SUB:
			result.result = argp->arg1 - argp->arg2;
			break;
		case MUL:
			result.result = argp->arg1*argp->arg2;
			break;
		case DIV:
			result.result =argp->arg1/argp->arg2;
			break;
		default:
			break;
	}
	/*
	 * insert server code here
	 */

	return &result;

 

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "math.h"


void
math_prog_2(char *host)
{
	CLIENT *clnt;
	struct MATH  *result_1;
	struct MATH  math_proc_2_arg;
	char c;
	printf("choose the operation:\n\t0------ADD\n\t1---------SUB\n\t2-------MUL\n\t3----DIV\n");
	c = getchar();
	switch(c){
		case '0':
			math_proc_2_arg.op=ADD;
			break;
		case '1':
			math_proc_2_arg.op =SUB;
			break;
		case '2':
			math_proc_2_arg.op =MUL;
			break;
		case '3':
			math_proc_2_arg.op =DIV;
			break;
		default:
			printf("error:operate\n");
			exit(1);
	}
	printf("input the first number:");
	scanf("%f",&math_proc_2_arg.arg1);
	printf("input the second number:");
	scanf("%f",&math_proc_2_arg.arg2);

#ifndef	DEBUG
	clnt = clnt_create (host, MATH_PROG, MATH_VER, "udp");
	if (clnt == NULL) {
		clnt_pcreateerror (host);
		exit (1);
	}
#endif	/* DEBUG */

	result_1 = math_proc_2(&math_proc_2_arg, clnt);
	if (result_1 == (struct MATH *) NULL) {
		clnt_perror (clnt, "call failed");
	}
#ifndef	DEBUG
	clnt_destroy (clnt);
#endif	 /* DEBUG */
	printf("The result is %.3f\n",result_1->result);
}


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

	if (argc < 2) {
		printf ("usage: %s server_host\n", argv[0]);
		exit (1);
	}
	host = argv[1];
	math_prog_2 (host);
exit (0);
}

你可能感兴趣的:(rpc编程(simple))