我试图创建一个C-ZeroMQ路由器,但似乎我正在制作它的方式存在问题。我的路由器似乎被初始化为null,这导致我的客户端代码的其余部分失败。关于为什么会有很多赞赏的提示。
我的程序基于CZMQ文件传输模型3代码,但当我在我的系统上运行时,我发现zsocket_new函数可以创建" router"似乎没有正确地创建"路由器,"所以当我断言(路由器)我的程序崩溃。
当我的程序在几行之后开始在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中加载程序,发现路由器保持初始化为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;
}
答案 0 :(得分:0)
尝试检查errno
或zmq_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的上下文无效。