c++ - zeromq:重置 REQ/REP 套接字状态

标签 c++ sockets zeromq

当您使用简单的 ZeroMQ REQ/REP 模式时,您依赖于固定的 send()->recv()/recv()->send() 序列。 作为this文章描述了当参与者在请求中间断开连接时你会遇到麻烦,因为这样你就不能从另一个连接接收下一个请求,但状态机会强制你向断开连接的请求发送请求。

自从提到的文章已经写完之后,是否出现了更优雅的方法来解决这个问题?

重新连接是解决这个问题的唯一方法(除了不使用 REQ/REP 而是使用其他模式)

最佳答案

由于接受的答案让我感到非常难过,所以我做了一些研究,发现我们需要的一切实际上都在文档中。

具有正确参数的 .setsockopt() 可以帮助您重置套接字状态机,而无需粗暴地破坏它并在前一个尸体之上重建另一个.

(是的,我喜欢这张图片)。

ZMQ_REQ_CORRELATE: match replies with requests
The default behaviour of REQ sockets is to rely on the ordering of messages to match requests and responses and that is usually sufficient. When this option is set to 1, the REQ socket will prefix outgoing messages with an extra frame containing a request id. That means the full message is (request id, 0, user frames…). The REQ socket will discard all incoming messages that don't begin with these two frames.
Option value type int
Option value unit 0, 1
Default value 0
Applicable socket types ZMQ_REQ

ZMQ_REQ_RELAXED: relax strict alternation between request and reply
By default, a REQ socket does not allow initiating a new request with zmq_send(3) until the reply to the previous one has been received. When set to 1, sending another message is allowed and has the effect of disconnecting the underlying connection to the peer from which the reply was expected, triggering a reconnection attempt on transports that support it. The request-reply state machine is reset and a new request is sent to the next available peer.
If set to 1, also enable ZMQ_REQ_CORRELATE to ensure correct matching of requests and replies. Otherwise a late reply to an aborted request can be reported as the reply to the superseding request.
Option value type int
Option value unit 0, 1
Default value 0
Applicable socket types ZMQ_REQ

A complete documentation is here

关于c++ - zeromq:重置 REQ/REP 套接字状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915347/

相关文章:

ZeroMQ主从双向通信

c++ - 创建MySQL无限循环

c++ - 简单C++继承的解释

c++ - 设置 Xcode 和 FLTK

java - 性能优化 : C++ vs Java not performing as expected

java - java(client-server)中如何使用wireshark检查与SSL sockets的连接等疑惑

python - socket.recvmsg 忽略 ancbufsize、辅助数据

java - 这是 websocket 编程的正确使用吗?

c# - ZeroMQ PUB/SUB 模式与多线程轮询取消

java - 向局域网上的所有人广播