我有一个任务的问题。我必须制作一个程序,使用RPC协议从远程主机接收时间和日期,并由rpcgen
生成。
rpc时间.x
program RPCTIME
{
version RPCTIMEVERSION
{
long GETTIME() = 1;
} = 1;
} = 2000001;
我用rpcgen -a rpctime.x
创建了这个文件
生成的文件(由我添加的行用注解//+
标记):
rpctime_server.c
#include "rpctime.h"
#include <time.h> //+
long *
gettime_1_svc(void *argp, struct svc_req *rqstp)
{
static long result;
time(&result); //+`
return &result;
}
rpctime_client.c
#include "rpctime.h"
#include "time.h" //+`
void
rpctime_1(char *host)
{
CLIENT *clnt;
long *result_1;
char buf[50];
char *gettime_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, RPCTIME, RPCTIMEVERSION, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = gettime_1((void*)&gettime_1_arg, clnt);
if (result_1 == (long *) NULL) {
clnt_perror (clnt, "call failed"); //here client program stops
} else {
printf("%s\n", ctime_r(result_1, buf)); //+`
}
#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];
rpctime_1 (host);
exit (0);
}
Makefile.rpctime
# This is a template Makefile generated by rpcgen
# Parameters
CLIENT = rpctime_client
SERVER = rpctime_server
SOURCES_CLNT.c =
SOURCES_CLNT.h =
SOURCES_SVC.c =
SOURCES_SVC.h =
SOURCES.x = rpctime.x
TARGETS_SVC.c = rpctime_svc.c rpctime_server.c
TARGETS_CLNT.c = rpctime_clnt.c rpctime_client.c
TARGETS = rpctime.h rpctime_clnt.c rpctime_svc.c rpctime_client.c rpctime_server.c
OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o)
OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o)
# Compiler flags
CFLAGS += -g
LDLIBS += -ltirpc // here was generated -lnsl but it didn`t work
RPCGENFLAGS =
# Targets
all : $(CLIENT) $(SERVER)
$(TARGETS) : $(SOURCES.x)
rpcgen $(RPCGENFLAGS) $(SOURCES.x)
$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) $(TARGETS_CLNT.c)
$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) $(TARGETS_SVC.c)
$(CLIENT) : $(OBJECTS_CLNT)
$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS)
$(SERVER) : $(OBJECTS_SVC)
$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)
clean:
$(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC) $(CLIENT) $(SERVER)
我用make -f Makefile.rpctime
编译了这个程序。
我可以运行./rpctime_server
和./rpctime_client 127.0.0.1
,但客户端总是以以下错误消息结束:
呼叫失败:RPC:过程不可用
但是,服务器仍在运行
1条答案
按热度按时间7gs2gvoe1#
这个问题发生在Ubuntu 22. 04上,但是早期版本没有类似的问题。我在Ubuntu 21上测试过,都工作正常。