重新绑定套接字时地址使用错误

重新绑定套接字时地址使用错误

本文介绍了ZeroMQ:重新绑定套接字时地址使用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将一个 ZeroMQ 套接字绑定到一个端点并关闭该套接字之后,将另一个套接字绑定到同一个端点需要多次尝试.之前对 zmq_bind 的调用直到成功调用失败,并显示错误Address in use"(EADDRINUSE).

After binding a ZeroMQ socket to an endpoint and closing the socket, binding another socket to the same endpoint requires several attempts. The previous calls to zmq_bind up until the successful one fail with the error "Address in use" (EADDRINUSE).

下面的代码演示了这个问题:

The following code demonstrates the problem:

#include <cassert>
#include <iostream>

#include "zmq.h"

int main() {
    void *ctx = zmq_ctx_new();
    assert( ctx );
    void *skt;

    skt = zmq_socket( ctx, ZMQ_REP );
    assert( skt );
    assert( zmq_bind( skt, "tcp://*:5555" ) == 0 );
    assert( zmq_close( skt ) == 0 );

    skt = zmq_socket( ctx, ZMQ_REP );
    assert( skt );
    int fail = 0;
    while ( zmq_bind( skt, "tcp://*:5555" ) ) { ++fail; }
    std::cout << fail << std::endl;
}

我在 Windows XP SP3 上使用 ZeroMQ 4.0.3,编译器是 VS 2008.libzmq.dll 已使用提供的 Visual Studio 解决方案构建.

I'm using ZeroMQ 4.0.3 on Windows XP SP3, compiler is VS 2008. libzmq.dll has been built with the provided Visual Studio solution.

在执行调试"构建(上面的代码和 libzmq.dll 的代码)和使用发布"构建的 0 时,这会在此处打印 1.奇怪的是,当使用混合构建配置(Debug with Release lib)运行上面的代码时,fail 计数高达 6.

This prints 1 here when doing a "Debug" build (both of the code above and of libzmq.dll) and 0 using a "Release" build. Strange enough, when running the code above with mixed build configuration (Debug with Release lib), fail counts up to 6.

推荐答案

Pieter Hintjens 给了我提示 在邮件列表中:

Pieter Hintjens gave me the hint on the mailing list:

zmq_close 的调用启动套接字关闭.这是在一个由 ZeroMQ 启动的特殊收割者"线程中完成的,以异步和非阻塞地调用 zmq_close.请参阅关于 ZeroMQ 架构的白皮书中的收割者线程".

The call to zmq_close initiates the socket shutdown. This is done in a special "reaper" thread started by ZeroMQ to make the call to zmq_close asynchronous and non-blocking. See "The reaper thread" in a whitepaper about ZeroMQ's architecture.

上面的代码不会等待线程完成实际工作,因此端点不会立即可用.

The code above does not wait for the thread doing the actual work, so the endpoint will not become available immediately.

这篇关于ZeroMQ:重新绑定套接字时地址使用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:35