【问题标题】:Socket operation on non-socket, zmq options非套接字上的套接字操作,zmq 选项
【发布时间】:2014-09-15 18:39:49
【问题描述】:

每当我尝试在 zmq 中设置套接字选项时,我都会得到 Socket operation on non-socket

zmq::socket_t socket = new zmq::socket_t(*context,ZMQ_REP);
int64_t t = 1000;
socket->setsockopt(ZMQ_RCVTIMEO,&t,sizeof(t));
socket->bind("ipc:///tmp/zmqsocket");

谁能告诉我我做错了什么?

我正在使用带有 c++ 绑定的 ZeroMQ 4.0.4。

编辑:尝试在绑定之前/之后设置选项,没有任何改变。

【问题讨论】:

    标签: c++ sockets zeromq


    【解决方案1】:

    您应该为 ZMQ_RCVTIMEO 选项使用正确的类型,它使用 int(不是 int64)。

    来自 zmq 文档zmq-setsockopt

    ZMQ_RCVTIMEO: Maximum time before a recv operation returns with EAGAIN
    
    Sets the timeout for receive operation on the socket. If the value is 0, zmq_recv(3) will return immediately, with a EAGAIN error if there is no message to receive. If the value is -1, it will block until a message is available. For all other values, it will wait for a message for that amount of time before returning with an EAGAIN error.
    Option value type   int
    Option value unit   milliseconds
    Default value   -1 (infinite)
    Applicable socket types     all
    

    那么下面的代码就可以工作了:

    zmq::context_t context(1);
    zmq::socket_t socket(context,ZMQ_REP);
    int t = 1000;
    socket.bind("ipc:///tmp/zmqsocket");
    socket.setsockopt(ZMQ_RCVTIMEO,&t,sizeof(t));
    

    【讨论】:

      【解决方案2】:

      记住 API v2.1 以来的经验法则

      就 v2.1 而言,setsockopt() 联机帮助页警告 说:

           int zmq_setsockopt ( void       *socket,
                                int         option_name,
                                const void *option_value,
                                size_t      option_len
                                );
           Caution: All options,
                    with the exception of ZMQ_SUBSCRIBE,    ZMQ_UNSUBSCRIBE,
                                          ZMQ_LINGER,       ZMQ_ROUTER_MANDATORY,
                                          ZMQ_PROBE_ROUTER, ZMQ_XPUB_VERBOSE,
                                          ZMQ_REQ_CORRELATE,
                                      and ZMQ_REQ_RELAXED,
                    only take effect for subsequent socket bind/connects.
                    ^^^^                 ^^^^^^^^^^
      

      渴望与已发布的 ZeroMQ API 兼容的代码应调用 .setsockopt() 来设置 ZMQ_RCVTIMEO 之前 转到 .connect() / .bind() p>

      【讨论】:

      • 感谢您的反馈。 API 调用的顺序仍然很重要。保留问题跟踪的隔离步骤。到目前为止,您已经测试了什么来隔离根本原因——在代码的哪个确切点触发了错误?在 .bind() 上,在 .setsockopt() 上?你的应用依赖什么操作系统——Win、Linux 以及你的 /tmp/zmqsocket 实体是如何在那里预配置的?
      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 2014-09-10
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多