Linux编程之从零开始搭建RPC分布式系统(2)

3.rpcgen -Sc -o my_client.c my.x 生成my_client.c

使用该指令后我们就生成了客户端.c文件,这个文件很重要,因为以后我们做业务开发就在这里做,我们的调用都会从这里开始。

 

 my_client.c:

/* * 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 "my.h" void my_rpc_prog_1(char *host) { CLIENT *clnt; int *result_1; my_io_data_t my_rpcc_1_arg; #ifndef DEBUG clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS1, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ result_1 = my_rpcc_1(&my_rpcc_1_arg, clnt); if (result_1 == (int *) NULL) { clnt_perror (clnt, "call failed"); } #ifndef DEBUG clnt_destroy (clnt); #endif /* DEBUG */ } void my_rpc_prog_2(char *host) { CLIENT *clnt; my_io_data_t *result_1; my_io_data_t my_rpcc_2_arg; #ifndef DEBUG clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS2, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ result_1 = my_rpcc_2(&my_rpcc_2_arg, clnt); if (result_1 == (my_io_data_t *) 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]; my_rpc_prog_1 (host); my_rpc_prog_2 (host); exit (0); }

 

View Code

现在我们就可以在该文件编写客户端的代码了。

 

5.rpcgen -Ss -o my_server.c my.x生成文件my_server.c

使用该指令后我们就生成了服务器.c文件,这个文件很重要,因为以后我们做业务开发就在这里做,我们将在这里编写处理客户端请求的代码。

my_server.c:

/* * 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 "my.h" int * my_rpcc_1_svc(my_io_data_t *argp, struct svc_req *rqstp) { static int result; /* * insert server code here */ return &result; } my_io_data_t * my_rpcc_2_svc(my_io_data_t *argp, struct svc_req *rqstp) { static my_io_data_t result; /* * insert server code here */ return &result; }

View Code

6.在my_server.c和my_client.c添加测试代码

 

所有利用rpcgen生成的文件都已经生成完毕,接下来我们需要添加测试代码来验证该RPC骨架是否正常工作。

my_client.c:

/* * 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 "my.h" void my_rpc_prog_1(char *host) { CLIENT *clnt; int *result_1; my_io_data_t my_rpcc_1_arg; #ifndef DEBUG clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS1, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ result_1 = my_rpcc_1(&my_rpcc_1_arg, clnt); if (result_1 == (int *) NULL) { clnt_perror (clnt, "call failed"); } #ifndef DEBUG clnt_destroy (clnt); #endif /* DEBUG */ } void my_rpc_prog_2(char *host) { CLIENT *clnt; my_io_data_t *result_1; my_io_data_t my_rpcc_2_arg; #ifndef DEBUG clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS2, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ my_rpcc_2_arg.mtype = 3; my_rpcc_2_arg.len = 18; result_1 = my_rpcc_2(&my_rpcc_2_arg, clnt); if (result_1 == (my_io_data_t *) NULL) { clnt_perror (clnt, "call failed"); } fprintf(stderr,"recv msg from server! mtype:%d len:%d \n",result_1->mtype,result_1->len); #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]; //my_rpc_prog_1 (host); my_rpc_prog_2 (host); exit (0); }

View Code

值得注意的是,我们client使用的是UDP协议,当然我们用户也可以根据自己需要选用TCP协议进行开发。

my_server.c

/* * 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 "my.h" int * my_rpcc_1_svc(my_io_data_t *argp, struct svc_req *rqstp) { static int result; /* * insert server code here */ return &result; } my_io_data_t * my_rpcc_2_svc(my_io_data_t *argp, struct svc_req *rqstp) { static my_io_data_t result; /* * insert server code here */ printf("recv msg from client! len:%d, mt:%d \n",argp->len,argp->mtype); result.mtype = 33; result.len = 12; return &result; }

View Code

 

7.测试现象

编译这client和server

 

gcc -o client my_clnt.c my_client.c my_xdr.c

gcc -o server my_svc.c my_server.c my_xdr.c

 

我在主机172.0.5.183运行server程序,在172.0.5.183运行client程序,测试现象如下:

 

server端

Linux编程之从零开始搭建RPC分布式系统

client端

Linux编程之从零开始搭建RPC分布式系统

以上测试已经证明了我们创建的RPC是可以正常通信的,那我们继续在此框架下完善代码,构建出可供业务开发的分布式系统的系统框架。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a2cca3a65b5d109885d73763b806c863.html