# 4rpc - RPC example II finding prime numbers print a list of...

This preview shows pages 1–6. Sign up to view the full content.

RPC example II -- finding prime numbers /* print a list of primes between 1 and 1000 */ main() { int I, how_many, primes[1000]; how_many = find_primes(1, 1000, primes); for (I = 0; I < how_many; I++) printf(“%d is prime\n”, primes[I]); } /* Find all primes between min and max, return them in an array */ int find_primes(int min, int max, int *array) { int I, count = 0; for (I = min; I <= max; I++) if (isprime(I)) array[count++] = I; return count; } /* Return TRUE If n is prime */ int isprime(int n) { int I; for (I = 2; I*I <= n; I++){ if ((n % I) == 0) return 0; } return 1; } main( ) find_primes( ) is_prime( ) Client Server

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
primes.x const MAXPRIMES = 1000; struct prime_request{ int min; int max; }; struct prime_result{ int array<MAXPRIMES>; }; program PRIMEPROG{ version PRIMEVERS{ prime_result FIND_PRIMES(prime_request) = 1; } = 1; } = 0x32345678;
p_server.c #include <rpc/rpc.h> #include "primes.h" prime_result *find_primes_1(prime_request *request) { static prime_result result; static int prime_array[MAXPRIMES]; int i, count = 0; for (i = request->min; i <= request->max; i++) if (isprime(i)) prime_array[count++] = i; result.array.array_len = count; result.array.array_val = prime_array; return(&result); } int isprime(int n) { int i; for (i = 2; i*i <= n; i++){ if ((n % i) == 0) return 0; } return 1; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
p_client.c #include <rpc/rpc.h> #include "primes.h" main(int argc, char *argv[]) { int i; CLIENT *cl; prime_result *result; prime_request request; if (argc != 4){ printf("usage: %s host min max\n", argv[0]); exit(1); } cl = clnt_create(argv[1], PRIMEPROG, PRIMEVERS, "tcp"); if (cl == NULL){ clnt_pcreateerror(argv[1]); exit(2); } request.min = atoi(argv[2]); request.max = atoi(argv[3]); result = find_primes_1(&request, cl); if (result == NULL){ clnt_perror(cl, argv[1]); exit(3); } for (i = 0; i < result->array.array_len; i++) printf("%d is prime\n", result->array.array_val[i]); printf("count of primes found = %d\n", result->array.array_len); xdr_free(xdr_prime_result, result); clnt_destroy(cl); }
One More Example on RPC /* Program: shop.x */ /* Limitation of RPC: Remote Procedure Call just supports single parameter passing. Hence, one have to define a specific data structure for passing

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

4rpc - RPC example II finding prime numbers print a list of...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online