我正在尝试编写一个简单的RPC程序,它接受一个字符串作为参数,并将其长度返回给调用程序。我正在使用RPCGEN生成所需的存根。代码如下:p..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文件中。我做错了什么?
发布于 2015-03-01 05:32:29
挂起的原因是服务器正在侦听客户端连接。使用ctrl+c会终止服务器。可以通过在serverproc.c中添加printf语句来验证这一点,并检查在尝试客户端连接时是否在服务器控制台中打印该语句。另外,如果您的客户端似乎处于挂起状态,则很可能您尚未启动端口映射器。
启动端口映射程序的命令
OS命令Mac OS X
launchctl start com.apple.portmap Linux
/sbin/portmap*BSD
/usr/sbin/rpcbindhttps://stackoverflow.com/questions/10793727
复制相似问题