Server Side Program

The following is an example of the lowest layer of RPC on the server side program:

#define RMTPROGNUM   (u_long)0x3fffffffL
#define RMTPROGVER   (u_long)0x1L
#define LONGPROC   1
#define STRINGPROC 2

#define MAXLEN 100

#include <stdio.h>
#include <rpc\rpc.h>
#include <sys\socket.h>

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

{
     int rmtprog();
     SVCXPRT *transp;

...

/* create TCP transport handle */
     transp = svctcp_create(RPC_ANYSOCK, 1024*10, 1024*10);
/* or create UDP transport handle */
/*   transp = svcudp_create(RPC_ANYSOCK);  */
     if (transp == NULL)   /* check transport handle creation */
      {
        fprintf(stderr, "can't create an RPC server transport\n");
        exit(-1);
      }

/* If exists, remove the mapping of remote program and port */
     pmap_unset(RMTPROGNUM, RMTPROGVER);

/* register remote program (TCP transport) with local Portmapper */
     if (!svc_register(transp, RMTPROGNUM, RMTPROGVER, rmtprog,
                         IPPROTO_TCP))
/* or register remote program (UDP transport) with local Portmapper */
/*   if (!svc_register(transp, RMTPROGNUM, RMTPROGVER, rmtprog,*/
                      /* IPPROTO_UDP)) */
      {
       fprintf(stderr, "can't register rmtprog() service\n");
       exit(-1);
      }

     svc_run();
     printf("Error:svc_run should never reaches this point \n");
     exit(1);

}

rmtprog(rqstp, transp)           /* code for remote program */
struct svc_req *rqstp;
SVCXPRT *transp;
{
   long in_long,out_long;
   char buf[100], *in_string=buf, *out_string=buf;
...
   switch((int)rqstp->rq_proc)   /* Which procedure ? */
   {
    case NULLPROC:
         if (!svc_sendreply(transp,xdr_void, 0))
          {
           fprintf(stderr,"can't reply to RPC call\n");
           exit(-1);
          }
         return;

    case LONGPROC:
...
         /* Process the request */
         if (!svc_sendreply(transp,xdr_long,&out_long))
          {
           fprintf(stderr,"can't reply to RPC call\n");
           exit(-1);
          }
         return;

    case STRINGPROC:   /* send received "Hello" message back */
                                 /* to client */
         svc_getargs(transp,xdr_wrapstring,(char *)&in_string);
         strcpy(out_string,in_string);

         /* send a reply back to a RPC client */
         if (!svc_sendreply(transp,xdr_wrapstring,
                                          (char *)&out_string))
          {
           fprintf(stderr,"can't reply to RPC call\n");
           exit(-1);
          }
         return;
    case ... :
...
       /* Any Remote procedure in RMTPROGNUM program */
...
    default:
       /* Requested procedure not found */
       svcerr_noproc(transp);
       return;
    }
}

The following steps describe the lowest layer of RPC on the server side program: