我正在尝试将字符指针从 rpc 客户端发送到 rpcgen 中的服务器,下面是服务器和客户端程序
RPC gen 的 RPC 程序
struct clientinput {
char * optype;
char * word;
};
program DICTIONARY_PROG {
version DICTIONARY_VERS {
int USEDICTIONARY(clientinput) = 1;
} = 1;
} = 0x23451113;
RPC 服务器
#include "dictionary.h"
int *
usedictionary_1_svc(clientinput *argp, struct svc_req *rqstp)
{
static int result;
char *optype = (char*)malloc (10);
char *word = (char*)malloc (10);
char *a = argp->optype;
char *b = argp->word;
printf("Optype is %s\n", a);
printf("Word is %s\n", b);
printf("The optype is %s\n", strcpy(optype, argp->optype));
printf("The word is %s\n", strcpy(word, argp->word));
/*
* insert server code here
*/
return &result;
}
RPC 客户端
#include "dictionary.h"
void
dictionary_prog_1(char *host, char *optype, char *word)
{
CLIENT *clnt;
int *result_1;
clientinput usedictionary_1_arg;
//strcpy(usedictionary_1_arg.optype, optype);
//strcpy(usedictionary_1_arg.word, word);
usedictionary_1_arg.optype = optype;
usedictionary_1_arg.word = word;
printf("Optype input is %s\n",usedictionary_1_arg.optype);
printf("Word input is %s \n",usedictionary_1_arg.word);
#ifndef DEBUG
clnt = clnt_create (host, DICTIONARY_PROG, DICTIONARY_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = usedictionary_1(&usedictionary_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc, char *argv[])
{
char *host, *optype, *word;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
optype = argv[2];
word = argv[3];
dictionary_prog_1 (host,optype,word);
exit (0);
}
这是服务器端的输出
Optyep is a
Word is e
The optype is a
The word is e
我在这里遇到的问题是,只有我从服务器传递到客户端的字符指针的第一个字符被打印。我尝试了使用字符指针的可能组合,但找不到原因。那么有人可以帮我找出原因吗?
最佳答案
从文档来看,RPCGen 需要一些帮助来区分单字符参数和实际的字符数组,因为所有参数都作为指针传递。要让它知道您需要一个字符数组(也称为字符串),您需要在结构声明中使用 string
关键字,如下所示:
struct clientinput {
string optype<>;
string word<>;
};
关于c - 在 RPCGen 中将字符指针从客户端传递到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822436/