当前位置: 技术问答>linux和unix
关于rpc编程的问题。。。。。
来源: 互联网 发布时间:2015-10-17
本文导语: 我想把在一个客户机上输入一个命令,例如/client hostname 12,之后服务器上显示your input value = 12,然后给客户机返回一个值12+1,请问该代码有什么问题?? 服务器端代码: #include #include #include #include "rpc.h" un...
我想把在一个客户机上输入一个命令,例如/client hostname 12,之后服务器上显示your input value = 12,然后给客户机返回一个值12+1,请问该代码有什么问题??
服务器端代码:
#include
#include
#include
#include "rpc.h"
unsigned int nuser(unsigned int ui)
{
printf("haha,your input value = %dn",ui);
return (ui+1);
};
main()
{
registerrpc(DEMOPROG, DEMOVER, DEMOFUNC,
nuser, xdr_u_int, xdr_u_int);
svc_run(); /* Never returns */
fprintf(stderr, "Error: svc_run returned!n");
exit(1);
}
客户机端代码:
#include
#include
#include
#include "rpc.h"
main(argc, argv)
int argc;
char **argv;
{
unsigned long nusers=0;
int stat=0;
unsigned int val=0;
if (argc != 3) {
fprintf(stderr, "usage: nusers hostnamen");
exit(-1);
}
val = atoi(argv[2]);
printf("val = %dn",val);
if (stat = callrpc(argv[1],
DEMOPROG, DEMOVER, DEMOFUNC,
xdr_u_int,&val, xdr_u_int, &nusers) != 0) {
clnt_perrno(stat);
exit(1);
}
printf("%d users on %sn", nusers, argv[1]);
exit(0);
}
头文件rpc.h:
#define DEMOPROG 0x20000001
#define DEMOVER 0x1
#define DEMOFUNC 0x1
服务器端代码:
#include
#include
#include
#include "rpc.h"
unsigned int nuser(unsigned int ui)
{
printf("haha,your input value = %dn",ui);
return (ui+1);
};
main()
{
registerrpc(DEMOPROG, DEMOVER, DEMOFUNC,
nuser, xdr_u_int, xdr_u_int);
svc_run(); /* Never returns */
fprintf(stderr, "Error: svc_run returned!n");
exit(1);
}
客户机端代码:
#include
#include
#include
#include "rpc.h"
main(argc, argv)
int argc;
char **argv;
{
unsigned long nusers=0;
int stat=0;
unsigned int val=0;
if (argc != 3) {
fprintf(stderr, "usage: nusers hostnamen");
exit(-1);
}
val = atoi(argv[2]);
printf("val = %dn",val);
if (stat = callrpc(argv[1],
DEMOPROG, DEMOVER, DEMOFUNC,
xdr_u_int,&val, xdr_u_int, &nusers) != 0) {
clnt_perrno(stat);
exit(1);
}
printf("%d users on %sn", nusers, argv[1]);
exit(0);
}
头文件rpc.h:
#define DEMOPROG 0x20000001
#define DEMOVER 0x1
#define DEMOFUNC 0x1
|
函数nuser需要返回指针
改为
unsigned int* nuser(unsigned int* ui)
{
printf("haha,your input value = %dn",*ui);
*ui=*ui+1;
return ui;
};
就可以了
改为
unsigned int* nuser(unsigned int* ui)
{
printf("haha,your input value = %dn",*ui);
*ui=*ui+1;
return ui;
};
就可以了