非套接字上的套接字操作,zmq选项

Socket operation on non-socket, zmq options

本文关键字:套接字 zmq 选项 操作      更新时间:2023-10-16

每当我试图在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。

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

您应该为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));

记住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兼容的代码应在进入.connect()/.bind()

之前调用.setsockopt()以设置ZMQ_RCVTIMEO