RPC is short for Remote Procedure Call. It can implement network operations locally in the form of function calls, so that programmers can focus on business logic rather than the underlying data communication.
Here we will not explain the RPC principle in detail, but will use a simple echo server example to explain how to use RPC.
Sun RPC was the earliest implementation. Now it has been transplanted to most operating systems.
With RPC, programmers only need to focus on the format of data transmission between the customer and the server and how to locate the remote process, how the client calls local functions to call remote functions, and how the server implements the remote process.
1. Data Interaction between the client and the server
Data transmission between the customer and the server is achieved by defining a. X file. In this file, the programmer can define the data format for the interaction between the client and the server. For example, the echo. x content is as follows:
struct hello {char text[256];};struct hello_echo {char text_echo[256];};program ECHO_PROG {version ECHO_VERS {hello_echo ECHOPROC(hello) = 1;} = 1;} = 0x31230000;
Echo. X defines two struct, struct hello, which is the data sent by the client to the server and struct hello_echo, which is the content sent by the server to the client. The following defines a program echo_prog, which is used to locate the remote process. When the client sends data, the server needs to determine which process to call.
2. Client call Remote Process
The client uses a client handle to call the remote process:
CLIENT *cl;hello in;hello_echo *outp;if(argc != 3) {fprintf(stderr, "usage: client
First, define a client pointer and two parameters for sending and receiving data, and then create the client through the Server IP address, remote program name, and version number, copy the text in the command line to the hello in sending parameter, and call the remote process echoproc_1 (). The echoproc is the function name defined in program echo_prog, but all the letters are in lower case, 1 is the function version number echo_vers.
3. Server Remote Process Definition
The main program of the server is automatically generated through rpcgen. Programmers only need to define the remote process.
#include "echo.h"hello_echo *echoproc_1_svc(hello *inp, struct svc_req *rqstp){static hello_echo out;strncpy(out.text_echo, inp->text, strlen(inp->text));out.text_echo[strlen(inp->text)] = 0;return (&out);}
The Remote Procedure name called by the client is echoproc_1, while the remote procedure name actually executed by the server is echoproc_1_svc.
The parameters and return values of echoproc_1_svc () have been determined as long as the function body is written.
The function here is simple callback. Therefore, execute a strncpy and return the hello_echo address. Because it is the address of a local variable, static local variables are used.
4 program running
Run rpcgen on Echo. X to obtain the header file and the master program of the server:
rpcgen -C echo.x
Then compile the client and server respectively:
gcc -o client client.c echo_clnt.c echo_xdr.cgcc -o server echo_svc.c server_func.c echo_xdr.c
Execution Server:
./server
The following error occurs:
Cannot register service: RPC: Authentication error; why = Client credential too weak
This indicates an authentication error. You can execute the authentication with the root permission:
sudo ./server
Then execute the client:
./client 127.0.0.1 abc
If the third parameter is sent, the server returns the message.
5 Summary
RPC simplifies the process of developing network applications. As long as the communication data and the Remote Process of the server are defined, the local process can be called on the client, and the remote process can be automatically called in the RPC Runtime Library. This is very convenient for developing large-scale network applications.