|
Network Programming This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Unix Network Programming - RPC ProgrammingOverview: This tutorial is concerned with building a simple remote application using SUN RPC. You are required to practice what you learned about RPC programming in previous lecture. IntroductionThe remote application to be built is a simple prime number query that provides three basic functions:
isprime (the input number is prime or not),
largestprime (the largest prime number within the search limit), and
totalprime (total number of prime numbers within the search limit).
#include <stdio.h> #include <math.h> #define PRINT_HR {printf("================================================\n");} int isprime(int n) { int i,squareroot; if ((n%2)==0) return 0; if (n>10) { squareroot = (int) sqrt(n); for (i=3; i<=squareroot; i=i+2) if ((n%i)==0) return 0; return 1; } /* Assume first four primes are counted elsewhere. */ else return 0; } int largestprime(int limit) { int n; /* loop count */ int prime; /* most recent prime found */ for (n=11; n<=limit; n=n+2) if (isprime(n)) prime = n; return prime; } int totalprime(int limit) { int n; /* loop count */ int pc; /* number of prime number */ pc=4; /* Assume the primes less than 10 (2,3,5,7) are counted here */ for (n=11; n<=limit; n=n+2) if (isprime(n)) pc++; return pc; } int main(void) { long input, result; char choice[10]; while(1) { printf("\nPrime Number Information:\n"); printf("Please select one of the following functions:\n"); printf("1. The greatest prime number\n"); printf("2. Number of prime numbers\n"); printf("3. Is it prime?\n"); printf("4. Exit\n\n"); printf("Enter your choice: "); scanf("%s", choice); switch (atoi(choice)) { case 1: printf("Enter the limit for your search: "); scanf("%d", &input); result = largestprime(input); PRINT_HR; printf("The largest prime number (before %d) is %d\n", input, result); PRINT_HR; break; case 2: printf("Enter the limit for your search: "); scanf("%d", &input); result = totalprime(input); PRINT_HR; printf("Number of prime numbers (from 1 to %d) is %d\n", input, result); PRINT_HR; break; case 3: printf("Enter the integer to be determined: "); scanf("%d", &input); PRINT_HR; if (isprime(input)) { printf("Yes, %d is prime.\n", input); } else printf("No, %d is not prime.\n", input); PRINT_HR; break; case 4: break; } if (atoi(choice) == 4) break; } printf("Program terminated.\n"); exit(0); }
Activity 1
rpcgen pquery.x Activity 2
/* * pquery_proc.c - remote procedures; called by server stub. */ #include <math.h> #include <rpc/rpc.h>/* standard RPC include file */ #include "pquery.h"/* this file is generated by rpcgen */ int * isprime_1(n) int *n; { int i, squareroot; static int result_value;/* return value */ if ((*n == 2)||(*n == 3)||(*n == 5)||(*n == 7)) { result_value = 1; return &result_value; } if ((*n%2) == 0) { result_value = 0; return &result_value; } if (*n > 10) { squareroot = sqrt((float)*n); for (i = 3; i <= squareroot; i = i+2) if ((*n%i) == 0) { result_value = 0; return &result_value; } result_value = 1; return &result_value; } else { result_value = 0; return &result_value; } } int * largestprime_1(limit) int *limit; { int n; /* loop count */ int *fresult;/* function return result */ static int result_value;/* largest prime number */ result_value = 0; if (*limit >= 2) result_value = 2; if (*limit >= 3) result_value = 3; if (*limit >= 5) result_value = 5; if (*limit >= 7) result_value = 7; for (n = 11; n <= *limit;n = n+2) { fresult = isprime_1(&n); if (*fresult) result_value = n; } return(&result_value); } int * totalprime_1(limit) int *limit; { int n; /* loop count */ int *fresult;/* function return result */ static int result_value;/* number of prime number */ result_value = 0; if (*limit >= 2) result_value++; if (*limit >= 3) result_value++; if (*limit >= 5) result_value++; if (*limit >= 7) result_value++; for (n = 11; n <= *limit; n = n+2) { fresult = isprime_1(&n); if (*fresult) result_value++; } return(&result_value); }
cc –c pquery_proc.c pquery_proc.o
cc –c pquery_svc.c pquery_svc.o
cc –o server_pquery pquery_proc.o pquery_svc.o –lrpc -lm
Activity 3
#include <stdio.h> #include <rpc/rpc.h> #include "pquery.h" #define PRINT_HR {printf("================================================\n");} main(argc, argv) int argc; char *argv[]; { CLIENT *cl; int input; int *fresult; char choice[10]; char *server; if (argc !=2) { fprintf(stderr, "usage: %s <hostname of server>\n", argv[0]); exit(1); } server = argv[1]; // create the client handle // call the RPC accordingly printf("Program terminated.\n"); clnt_destroy(cl); exit(0); }
cc –c client_pquery.c client_pquery.o
cc –c pquery_clnt.c pquery_clnt.o
cc –o client_pquery client_pquery.o pquery_clnt.o -lrpc
Activity 4
./server_pquery &
./client_pquery localhost
Submission
|