- /*
- * 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"
- #include "rpc_msg.h"
- intmy_rpc_prog_1(char*host, my_io_data_t* in_msg)
- {
- CLIENT *clnt;
- int*result_1;
- #ifndef DEBUG
- clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS1,"udp");
- if(clnt == NULL)
- {
- printf("Fail to create rpc client1!\n");
- return-1;
- }
- #endif/* DEBUG */
- result_1 = my_rpcc_1(in_msg, clnt);
- if(result_1 == (int*) NULL)
- {
- clnt_perror (clnt, "call failed");
- return-1;
- }
- return 0;
- }
- my_io_data_t * my_rpc_prog_2(char*host, my_io_data_t* in_msg)
- {
- CLIENT *clnt;
- my_io_data_t *result_1 = NULL;
- #ifndef DEBUG
- clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS2,"udp");
- if(clnt == NULL)
- {
- printf("Fail to create rpc client1!\n");
- return NULL;
- }
- #endif/* DEBUG */
- result_1 = my_rpcc_2(in_msg, clnt);
- if(result_1 == (my_io_data_t *) NULL)
- {
- clnt_perror (clnt, "call failed");
- return NULL;
- }
- return result_1;
- }
- voidget_compute_result(char*host,inttype,intpara1,int para2)
- {
- my_io_data_t in_msg;
- my_msg_t* rsp;
- my_io_data_t* out_msg;
- my_msg_t* req = (my_msg_t*)&in_msg;
- memset(&in_msg,0,sizeof(in_msg));
- req->msg_hdr.mtype = type;
- req->msg_hdr.len =sizeof(in_msg) -sizeof(my_msg_hdr_t);
- req->para1 = para1;
- req->para2 = para2;
- out_msg = my_rpc_prog_2(host, &in_msg);
- rsp = (my_msg_t*)out_msg;
- if(rsp == NULL)
- {
- printf("RPC call fail!\n");
- return;
- }
- printf("compute result is %d\n",rsp->result);
- }
- voidserver_switch(char*host,int type)
- {
- my_io_data_t msg;
- my_msg_t* in_msg = (my_msg_t*)&msg;
- memset(&msg,0,sizeof(msg));
- in_msg->msg_hdr.mtype = type;
- in_msg->msg_hdr.len =sizeof(msg) -sizeof(my_msg_hdr_t);
- if(my_rpc_prog_1(host, &msg))
- {
- printf("enable server fail!\n");
- }
- printf("Configure server successfully!\n");
- }
- intmain (intargc,char*argv[])
- {
- server_switch(SERVER_IP, RPC_enable); //server startsleep(1);
- get_compute_result(SERVER_IP, RPC_ADD, 6,3);
- sleep(1);
- get_compute_result(SERVER_IP, RPC_SUB, 6,3);
- sleep(1);
- get_compute_result(SERVER_IP, RPC_MUL, 6,3);
- sleep(1);
- get_compute_result(SERVER_IP, RPC_DIV, 6,3);
- sleep(1);
- server_switch(SERVER_IP, RPC_disable); //server close
- return 0;
- }
来源: