gpt4 book ai didi

c - 从多线程 RPC 服务器返回带有字符串的结构

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:26 25 4
gpt4 key购买 nike

我正在尝试编写一个返回结构的多线程 RPC 服务器。这是我的 XDR 文件。我正在运行 rpcgen -MN foo.x 来生成多线程兼容代码。

// foo.x
struct foo_out {
string name<128>;
};

program FOO_PROG {
version FOO_VERS {
foo_out foo(void) = 2;
} = 2 ;
} = 0x31230000;

这是我的服务器,您可以看到它采用指向我的结构的指针,为名称字符串分配一些内存,然后复制该字符串。我还添加了调试输出

// foo_server.c
#include "foo.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>

bool_t foo_2_svc(foo_out *out, struct svc_req *req) {
out->name = malloc(sizeof("foo"));
strcpy(out->name, "foo");
printf("Value: '%s'\n", out->name);
return TRUE;
}

int
foo_prog_2_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result) {
xdr_free(xdr_result, result);
return(1);
}

这是客户。它创建一个变量,传入一个指向该变量的指针,然后输出值:

// foo_client.c
#include "memory.h" /* for memset */
#include "foo.h"
#include "stdio.h"
#include "stdlib.h"
#include "rpc/pmap_clnt.h"
#include "string.h"
#include "memory.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include <unistd.h>

int
main (int argc,char **argv)
{
CLIENT *cl;

cl = clnt_create("127.0.0.1", FOO_PROG, FOO_VERS, "tcp");
if (cl == NULL) {
clnt_perror(cl, "call failed");
exit (1);
}

foo_out out;
if (foo_2(&out, cl) != RPC_SUCCESS) {
printf("failed \n");
// exit(1);
}
printf("foo out: %s\n", out.name);

sleep(1);
exit(0);
}

当我运行它时,服务器正常我每次通过客户端调用它时都会看到 Value: 'foo'。但是,客户端因 segv 而崩溃。使用地址 sanitizer 我得到这个:

=================================================================
==8269==ERROR: AddressSanitizer: SEGV on unknown address 0x0000004b0503 (pc 0x7fc9e2b5160f bp 0x000000000003 sp 0x7ffe264190c0 T0)
#0 0x7fc9e2b5160e in xdr_string (/lib/x86_64-linux-gnu/libc.so.6+0x13b60e)
#1 0x460751 in __interceptor_xdr_string.part.268 (/vagrant/foo/foo_client+0x460751)
#2 0x4b04a7 in xdr_foo_out /vagrant/foo/foo_xdr.c:13
#3 0x7fc9e2b4b3ea (/lib/x86_64-linux-gnu/libc.so.6+0x1353ea)
#4 0x4b03b8 in foo_2 /vagrant/foo/foo_clnt.c:15
#5 0x4b042e in main /vagrant/foo/foo_client.c:28
#6 0x7fc9e2a3682f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#7 0x405298 in _start (/vagrant/foo/foo_client+0x405298)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV ??:0 xdr_string
==8269==ABORTING

当我使用 GDB 检查 foo_2 调用前后结构的内容时,它没有改变。看起来服务器根本没有修改该值。

问题:如何使用多线程 RPC 调用修改带有字符串的结构?如果我将示例更改为使用整数而不是字符串,它就可以正常工作。所以看起来我缺少一些基本的东西。关于我做错了什么或调试此步骤的任何想法?

最佳答案

在您的客户端尝试为 foo_out 结构分配内存:

foo_out *out = malloc(sizeof(foo_out));
if (foo_2(out, cl) != RPC_SUCCESS) {
printf("failed \n");
// exit(1);
}
printf("foo out: %s\n", out->name);

关于c - 从多线程 RPC 服务器返回带有字符串的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com