Recent Changes - Search:

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.

Unix Network Programming - RPC Programming

Overview: 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.


Introduction

The 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).
  • The following shows you a source code of that program which the three functions are performed locally.
#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);
}
  • In this tutorial, you will be asked to convert the above simple program into a RPC version such that the aforementioned functions are performed remotely by a server program.
  • Specifically, you are required to write a client program that calls remote services using RPC. The client program passes an integer value to a remote server, and the server returns the result value.
  • The server program provides the three prime number query functions.
  • In addition, you will be asked to define the RPC specification file for this application, as well as the steps involved in building the client executable and the server executable.

Activity 1

  • Write a RPC specification file, named pquery.x, to define the server procedures along with their arguments and results. Name the three concerned procedures to be ISPRIME, LARGESTPRIME and TOTALPRIME, respectively.
  • Use rpcgen to compile pquery.x
        rpcgen pquery.x

Activity 2

  • Study the RPC server program, named pquery_proc.c, which provides the three functions: isprime, largestprime and totalprime.
/*
 * 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);
}
  • Compile the RPC server program with the following commands:
        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

  • Based on the given source code of the program, write a RPC client program where the query functions are performed and returned by the RPC server program.
  • The following program skeleton may be useful to you:
#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);
}
  • Compile the RPC client program with the following commands:
        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

  • Run and test the RPC system with the following commands:
        ./server_pquery &
        ./client_pquery localhost

Submission

  • You need to demonstrate your work to Steven in a tutorial session. Steven may ask you some questions to test your knowledge.

Edit - History - Print - Recent Changes - Search
Page last modified on December 09, 2009, at 05:27 PM