问题
I'm trying to write the simpliest client in RPC with this code:
#include <stdio.h>
#include <stdlib.h>
#include <rpc/rpc.h>
int main(int argc, char *argv[]){
int stat;
char out;
char in='f';
if(stat=callrpc(argv[1],0x20000001, 1, 1, (xdrproc_t)xdr_void, &in, (xdrproc_t)xdr_char, &out)!=0){
clnt_perrno(stat);
exit(1);
}
exit(0);
}
It compiles, but when I try to run it, it gives me a "RPC: Can't encode arguments"
EDIT: Actually the server do not recieve any argument neither it send back anything, that's why I put a xdr_void
added &in
and &out
to avoid segmentation fault error.
回答1:
You are missing some parentheses:
if (stat = callrpc(...) != 0)
is evaluated to
if (stat = (callrpc(...) != 0))
which always assigns 1
to stat
in case of an error, which is RPC_CANTENCODEARGS
. You need
if ((stat = callrpc(...)) != 0)
to get the real error code and message printed in
clnt_perrno(stat);
来源:https://stackoverflow.com/questions/35451760/rpc-client-gives-cant-encode-arguments