gpt4 book ai didi

CZMQ "zsocket_new"路由器创建始终为 NULL?

转载 作者:行者123 更新时间:2023-11-30 16:57:50 25 4
gpt4 key购买 nike

我正在尝试创建一个 C-ZeroMQ 路由器,但我的制作方式似乎有问题。我的路由器似乎被初始化为空,这导致我的客户端代码的其余部分失败。任何有关原因的提示将不胜感激。

我的程序基于 CZMQ 文件传输模型 3 代码,但是当我在系统上运行它时,我发现创建“路由器”的 zsocket_new 函数似乎没有正确创建“路由器”,所以当我断言(路由器)我的程序崩溃了。

当我的程序在几行后的 zsocket_set_hwm() 函数处开始崩溃时,我最初发现了这一点。我通过检查日志进行调查,发现我的程序在管理完整的“断言路由器”之前停止了:

 : zmq: attempting to open target file
: zmq: asserting ctx...
: zmq: asserted ctx...
: zmq: router value = 6
: zmq: created router
: zmq: asserting router......

接下来我在GDB中加载程序,发现router一直初始化为0x0。

(gdb) print ctx
$1 = <optimized out>
(gdb) print router
$2 = (void *) 0x0
(gdb) print complete_address
$3 = 0x7fffe80009a0 "tcp://127.0.0.1:6000"

声明路由器时似乎存在问题,但我的代码可能出了什么问题?我正在使用相应的客户端程序,它根本没有报告任何问题(这是其中的一部分):

zctx_t *ctx = zctx_new ();
void *dealer = zsocket_new (ctx, ZMQ_DEALER);

fprintf(fp_srv_log, "%s : %s\n", time_str, "ZMQ client thread launched successfully");

zsocket_bind (dealer, "tcp://*:6000");

fprintf(fp_srv_log, "%s : %s\n", time_str, "client: bound tcp://*:6000");

这是有问题的代码,服务器:

int send_file( void *args, zctx_t *ctx, void *pipe )
{
time_t raw_time;
struct tm* timeinfo;

struct arg_struct *input = (struct arg_struct *)args;
const char *fp = input->file_name;

time( &raw_time );
timeinfo = localtime( &raw_time );
char * time_str = asctime( timeinfo );
char * complete_address = "tcp://127.0.0.1:6000";
FILE *fp_clnt_log = fopen( "/var/log/myLog.log", "w" );

int chunkNum = 0;
fprintf( fp_clnt_log, "%s : zmq: attempting to open target file \n", time_str );
FILE *file_to_xfer = fopen( fp, "r" );
assert( file_to_xfer );

time_str = asctime( timeinfo );

fprintf( fp_clnt_log, "%s : zmq: asserting ctx...\n", time_str );
assert( ctx );
fprintf( fp_clnt_log, "%s : zmq: asserted ctx...\n", time_str );
fprintf( fp_clnt_log, "%s : zmq: router value = %d\n", time_str, ZMQ_ROUTER );
void *router = zsocket_new (ctx, ZMQ_ROUTER);
fprintf( fp_clnt_log, "%s : zmq: created router\n", time_str );
fprintf( fp_clnt_log, "%s : zmq: asserting router......\n", time_str );
assert( router );
fprintf( fp_clnt_log, "%s : zmq: asserted router successfully.\n", time_str );

//two parts per msg so HWM is size PIPELINE * 2
zsocket_set_hwm (router, PIPELINE * 2);
fprintf( fp_clnt_log, "%s : zmq: set hwm complete\n", time_str );

fprintf( fp_clnt_log, "%s : zmq: attempting to connect to %s\n", time_str, complete_address );
if( 0 == zsocket_connect (router, complete_address ) )
{
fprintf( fp_clnt_log, "%s : zmq: connected to %s\n", time_str, complete_address );
}
else
{
fprintf( fp_clnt_log, "%s : zmq: failed to connect to %s\n", time_str, complete_address );
}
while (true)
{
time( &raw_time );
timeinfo = localtime( &raw_time );
time_str = asctime( timeinfo );

//first frame in each message is the sender identity
fprintf( fp_clnt_log, "%s : zmq:frame 1\n", time_str );
zframe_t *identity = zframe_recv( router );
fprintf( fp_clnt_log, "%s : zmq clnt: checking identity...\n", time_str );
if (!identity)
{
fprintf( fp_clnt_log, "%s : zmq clnt: no identity, breaking.\n", time_str );
break; //shut down and quit
}

fprintf( fp_clnt_log, "%s : zmq:frame 2\n", time_str );
//second frame is 'fetch' command
char *command = zstr_recv (router);
assert (streq (command, "fetch"));
free (command);

fprintf( fp_clnt_log, "%s : zmq:frame 3\n", time_str );
//third frame is chunk offset in file
char *offset_str = zstr_recv (router);
size_t offset = atoi (offset_str);
free (offset_str);

fprintf( fp_clnt_log, "%s : zmq:frame 4\n", time_str );
//fourth frame is max chunk size
char *chunksz_str = zstr_recv (router);
size_t chunksz = atoi (chunksz_str);
free (chunksz_str);

fprintf( fp_clnt_log, "%s : zmq: reading chunk\n", time_str );
//read chunk of data from file
fseek (file_to_xfer, offset, SEEK_SET);
byte *data = malloc (chunksz);
assert (data);

fprintf( fp_clnt_log, "%s : zmq: sending chunk\n", time_str );
//send resulting chunk to client
size_t size = fread (data, 1, chunksz, file_to_xfer);
zframe_t *chunk = zframe_new (data, size);
zframe_send (&identity, router, ZFRAME_MORE);
zframe_send (&chunk, router, 0);
//printf("Server: Sending chunk %d\n", chunkNum);
chunkNum++;
}

time( &raw_time );
timeinfo = localtime( &raw_time );
time_str = asctime( timeinfo );

fprintf( fp_clnt_log, "%s : closing file\n", time_str );
fclose( file_to_xfer );
fclose( fp_clnt_log );

return 0;

}

最佳答案

尝试检查 errnozmq_errno() 的值并与 zmq_strerror() 一起使用

根据文档,如果套接字创建失败,则返回 NULL,并且 errno 将设置为以下之一:

EINVAL - requested socket type is invalid.
EFAULT - provided context is invalid.
EMFILE - limit on the total number of open ØMQ sockets has been reached.
ETERM - context specified was terminated.

我猜最有可能的情况是传递给 send_file 的上下文无效。

关于CZMQ "zsocket_new"路由器创建始终为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284428/

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