我正在尝试编写一个简单的 RPC 程序,它将字符串作为参数并将其长度返回给调用程序。我正在使用 RPCGEN 生成所需的存根。代码如下: prog.x 文件:
program PROGRAM_PROG {
version PROGRAM_VERS {
int fun(string) = 1; /* procedure number = 1 */
} = 1; /* version number = 1 */
} = 0x31234567; /* program number = 0x31234567 */
client.c 文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <rpc/rpc.h> /* standard RPC include file */
#include "prog.h" /* this file is generated by rpcgen */
#include "math.h"
main(int argc, char *argv[])
{
CLIENT *cl; /* RPC handle */
char *server;
char *word;
int *lresult; /* return value from bin_date_1() */
if (argc != 3) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);
exit(1);
}
server = argv[1];
word = argv[2];
printf("%s======\n", server);
printf("%s======\n", word);
/*
* Create client handle
*/
if ((cl = clnt_create(server, PROGRAM_PROG, PROGRAM_VERS, "udp")) == NULL) {
/*
* can't establish connection with server
*/
clnt_pcreateerror(server);
exit(2);
}
/*
* First call the remote procedure "bin_date".
*/
printf("123\n");
if ( (lresult = fun_1(&word, cl)) == NULL) {
clnt_perror(cl, server);
exit(3);
}
printf("456\n");
printf("time on host %s = %d and look, word: %s\n",server, *lresult, word);
clnt_destroy(cl); /* done with the handle */
exit(0);
}
最后是 serverproc.c 文件:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <rpc/rpc.h>
#include "prog.h"
int *fun_1(char **p, CLIENT *cl)
{
int len;
int t;
t = strlen(*p);
return &t;
}
int * fun_1_svc(char **p, struct svc_req *cl){
CLIENT *client;
return(fun_1(0, client));
}
代码编译得很好,但是在运行它时,它只是挂起,我必须使用 Ctrl-C 来结束它。问题可能出在 serverproc.c 文件中。我究竟做错了什么?