Note: Make Sure you had done following before proceeding
sudo apt-get install rpcbind
Then Create Directory With Any Name As Follow
mkdir rpcprogram
then in the directory using following command
cd rpcprogram
Follow the step as given below
1) Creating file with .x extension
I am using gedit editor
so
gedit fact.x
2) Add following code to fact.x
struct intpair
{
int a;
};
program FACT_PROG
{
version FACT_VERS
{
int FACT(intpair) = 1;
} = 1;
} = 0x23451111;
Save and Exit File
3) We Need RPC to generate C Code to implement RPC protocol
use the following command with your .x file name
rpcgen -a -C fact.x
4) Edit the Makefile
gedit Makefile.fact
5) Search for following Lines in File
CFLAGS += -g
Change it to:
CFLAGS += -g -DRPC_SVC_FG
6) Again search the following and replace
RPCGENFLAGS =
Change it to:
RPCGENFLAGS = -C
# save and exit the file
7) Client program
gedit fact_client.c
(Note the changes in the following Program )
#include "fact.h"
void fact_prog_1(char *host,int a)
{
CLIENT *clnt;
int *result_1;
intpair fact_1_arg;
#ifndefDEBUG
clnt = clnt_create (host, FACT_PROG, FACT_VERS, "udp");
if (clnt == NULL)
{
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
fact_1_arg.a=a;
result_1 = fact_1(&fact_1_arg, clnt);
if (result_1 == (int *) NULL)
{
clnt_perror (clnt, "call failed");
}
else
{
printf("Factorial=%d",*result_1);
}
#ifndefDEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int main (int argc, char *argv[])
{
char *host;
int a,ch;
if (argc < 2)
{
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
do
{
system("clear");
printf("\nEnter a no:: ");
scanf("%d",&a);
fact_prog_1 (host,a);
printf("\nTry again : (1/0) :: ");
scanf("%d",&ch);
} while(ch==1);
exit (0);
}
8) Server Program
gedit fact_server.c
(Note the changes in the following Program )
sudo apt-get install rpcbind
Then Create Directory With Any Name As Follow
mkdir rpcprogram
then in the directory using following command
cd rpcprogram
Follow the step as given below
1) Creating file with .x extension
I am using gedit editor
so
gedit fact.x
2) Add following code to fact.x
struct intpair
{
int a;
};
program FACT_PROG
{
version FACT_VERS
{
int FACT(intpair) = 1;
} = 1;
} = 0x23451111;
Save and Exit File
3) We Need RPC to generate C Code to implement RPC protocol
use the following command with your .x file name
rpcgen -a -C fact.x
4) Edit the Makefile
gedit Makefile.fact
5) Search for following Lines in File
CFLAGS += -g
Change it to:
CFLAGS += -g -DRPC_SVC_FG
6) Again search the following and replace
RPCGENFLAGS =
Change it to:
RPCGENFLAGS = -C
# save and exit the file
7) Client program
gedit fact_client.c
(Note the changes in the following Program )
void fact_prog_1(char *host,int a)
{
CLIENT *clnt;
int *result_1;
intpair fact_1_arg;
#ifndefDEBUG
clnt = clnt_create (host, FACT_PROG, FACT_VERS, "udp");
if (clnt == NULL)
{
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
fact_1_arg.a=a;
result_1 = fact_1(&fact_1_arg, clnt);
if (result_1 == (int *) NULL)
{
clnt_perror (clnt, "call failed");
}
else
{
printf("Factorial=%d",*result_1);
}
#ifndefDEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int main (int argc, char *argv[])
{
char *host;
int a,ch;
if (argc < 2)
{
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
do
{
system("clear");
printf("\nEnter a no:: ");
scanf("%d",&a);
fact_prog_1 (host,a);
printf("\nTry again : (1/0) :: ");
scanf("%d",&ch);
} while(ch==1);
exit (0);
}
8) Server Program
gedit fact_server.c
(Note the changes in the following Program )
#include "fact.h"
int *fact_1_svc(intpair *argp, struct svc_req *rqstp)
{
static int result,n,fact;
int i;
n=argp->a;
// factorial logic
fact = 1;
printf("\n Received : n= %d \n",n);
for (i=n;i>0;i--)
{
fact=fact * i;
}
result=fact;
return &result;
}
9) Run Programs
# In terminal 1, run:
sudo ./fact_server
# In terminal 2, run:
./fact_client localhost
Output:
humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ make -f Makefile.fact
cc -g -DRPC_SVC_FG -c -o fact_clnt.o fact_clnt.c
cc -g -DRPC_SVC_FG -c -o fact_client.o fact_client.c
cc -g -DRPC_SVC_FG -c -o fact_xdr.o fact_xdr.c
cc -g -DRPC_SVC_FG -o fact_client fact_clnt.o fact_client.o fact_xdr.o -lnsl
cc -g -DRPC_SVC_FG -c -o fact_svc.o fact_svc.c
cc -g -DRPC_SVC_FG -c -o fact_server.o fact_server.c
cc -g -DRPC_SVC_FG -o fact_server fact_svc.o fact_server.o fact_xdr.o -lnsl
humble@humble-desktop:~/exp2$
At server:
humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ sudo ./fact_server
Received : n= 5
At client:
humble@humble-desktop:~$ cd exp2
humble@humble-desktop:~/exp2$ ./fact_client localhost
Enter a no:: 5
Factorial=120
Try again : (1/0) ::