Wednesday, February 10, 2016

How to create RPC program?

RPC ( Remote Procedural Call) is providing platform to use remote method. RPC program required rpcbind package for execution.

Step-1 : Create .x file

First we have to create .x file with required parameters like procedure declaration, version number etc.

Program Number

This program number is permit to implementation of remote procedure. A program number is assigned by groups of 0x20000000 (decimal 536870912), according to the following list:
Number
Detail
0-1xxxxxxx This group of numbers is predefined and administered by the operating system. The numbers should be identical for all system customers.
20000000-3xxxxxxx The user defines this group of numbers. The numbers are used for new applications and for debugging new programs.
40000000-5xxxxxxx This group of numbers is transient and is used for applications that generate program numbers dynamically.
All other are reserved.

Version Numbers

The version number identifies which version of the protocol the caller is using. The first implementation of a remote program is usually designated as version number 1.

Procedure Numbers

The procedure number (proc parameter) identifies the procedure to be called.
Example: first.x
program first
{
        version vers
        {
                int dtow(int)=1; //procedure number
        }=1; //version number
}=0x35353535; // program number

Here first is same as filename. int dtow(int) is function declaration with return type integer and single argument return type integer. 0x35353535 is program number.

Step-2: Run .x file using rpcgen command

Open terminal and execute following command
[root@server-host ~] rpcgen –a first.x
This command will create six file shown as following.
  • first_client.c     (Client File)
  • first_clnt.c       (Client Stub)
  • first_server.c    (Server File)
  • first_svc.c        (Server Stub)
  • first.h               (Header File)
  • makefile.first   (Make File)

Step-3: Start rpcbind service

Before start rpcbind service login into administrator user using following command.
[root@server-host ~] sudo su
After enter user password now you are able to work as administrator user.
Start rpcbind service using following command
[root@server-host ~] service rpcbind start

Step-4: Modify Client file

Open Client file named first_client.c and modify required code.  
Then compile client file using follow command
[root@server-host ~] gcc –o first_client first_client.c
This command will compile client file and create executable file named first_client.

Step-5: Modify Server file

Open Server file named first_server.c and modify required code.
Then compile client file using follow command
[root@server-host ~] gcc –o first_server first_server.c
This command will compile server file and create executable file named first_server.

Step-6: Run Server

Run server using following command.
[root@server-host ~]./first_server
This command starts the server and keep server ready to provide service.

Step-7: Run Client

Run server using following command.
[root@server-host ~]./first_client server_host
If server_host is run in same machine then server_host is known as localhost.
After this client will send request to server and server will reply using its services.

No comments:

Post a Comment