RPC stand for Remote procedure call.
We will see how to run an RPC program in Ubuntu.
Open the Terminal and type rpcinfo
If you get error you must type the following command
Note:- You must connect to the internet before type he following command.
sudo apt-get install rpcbind
To verify the installation retype rpcinfo
If you got the .following details.
Now you can proceed.
What is our program do.
- Simply it is send the First and second initials in your name.
- Then server will print the both Initials.
- After that server replies how many messages server handle so far (with this message).
- Client will print it.
Now open text Editor and add the following code to a blank file.
struct messages{
char firstinitial;
char secondinitial;
};
program HELLO {
version HELLO_1{
int MY_FUNCTION(messages)=1;
}=1;
}=0x2fffffff;
We can use only one parameter for a function. So when we want to send 2 parameters simply we do is make structure (Class in java )which has number of parameters. So I need to send 2 parameters. Then I made a structure with 2 character parameters.
program HELLO
this means the programs name. It must be capital.
version HELLO_1
This must be a capital.
int MY_FUNCTION(messages)=1;
This is the method we going to call remotely.
int - return type
MY_FUNCTION - function name
messages - type of the parameter
1 - number of the function. (I will give this as method number 1)
=1;
Version of the program
=0x2fffffff;
Program number.
Then save it as hello.x
by using Terminal go to the folder where the hello.x is located.
Then type following command
rpcgen -a -C hello.x
You can see some files were created in the folder where the hello.x is located.
Now open the hello_client.c file in that folder.
It must be something like this.
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "hello.h"
void
hello_1(char *host)
{
CLIENT *clnt;
int *result_1;
messages my_function_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, HELLO, HELLO_1, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = my_function_1(&my_function_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
hello_1 (host);
exit (0);
}
my_function_1_arg -- Client must put data to here for send to server.
*result_1 -- Servers reply will catch by this variable.
result_1 = my_function_1(&my_function_1_arg, clnt); --- Where the function calls and get the reply
Now add the following sections.
my_function_1_arg.firstinitial='L';
my_function_1_arg.secondinitial='J';
This section must add before the very first line of #ifndef DEBUG line.
Note:- you can add this line any where you want befor calling my_function1
printf("You connect with server for %d time",result_1[0]);
This section must add before the clnt_destroy (clnt); line.
Note:- you can add this line any where you want after calling my_function1
Now save the file.
AFTER EDIT CLIENT CODE LOOKS LIKE THIS
/*
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "hello.h"
void
hello_1(char *host)
{
CLIENT *clnt;
int *result_1;
messages my_function_1_arg;
my_function_1_arg.firstinitial='L';
my_function_1_arg.secondinitial='J';
#ifndef DEBUG
clnt = clnt_create (host, HELLO, HELLO_1, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = my_function_1(&my_function_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("You connect with server for %d time",result_1[0]);
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
hello_1 (host);
exit (0);
}
Now open the hello_server.c file.
It must look like this.
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "hello.h"
int calls=0;
int *
my_function_1_svc(messages *argp, struct svc_req *rqstp)
{
static int result;
calls++;
/*
* insert server code here
*/
printf("Full initials are :- %c and %c \n",argp->firstinitial,argp->secondinitial);
result=calls;
return &result;
}
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "hello.h"
void
hello_1(char *host)
{
CLIENT *clnt;
int *result_1;
messages my_function_1_arg;
my_function_1_arg.firstinitial='L';
my_function_1_arg.secondinitial='J';
#ifndef DEBUG
clnt = clnt_create (host, HELLO, HELLO_1, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = my_function_1(&my_function_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("You connect with server for %d time",result_1[0]);
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
hello_1 (host);
exit (0);
}
Then in terminal enter the following command.
make -f Makefile.hello
Then new files will create in the folder.
Then type sudo ./hello_server .
Now open a new terminal and go to the folder where the hello.x is located.
Now type sudo ./hello_client localhost .
local host means server's address.
You can see the following data in both terminals as a result
Do the sudo ./hello_client localhost again and see the results.
well done bro :) It will be very helpful for us.. thank you
ReplyDeleteThis is very Useful!Keep it up bro :)
ReplyDeleteThanks bro. It is really good example. Well explained how it works. :)
ReplyDeleteThanks bro !!! nice work (y)
ReplyDeleteNicely explained bro!!! (Y) Thanks for sharing :)
ReplyDeleteThank u Nithila (y) (y). It is clearly explained. =D
ReplyDeleteThanks bro
ReplyDeleteKeep up the good work