gpt4 book ai didi

java - ZeroMQ 跨语言消息翻译 CPPZMQ -- JeroMQ -- PyZMQ

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:30 29 4
gpt4 key购买 nike

在 C++ ZMQ Publisher 中,我将字符串打包到 message_t 中,如下所示:

int main()
{
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_PUB);
socket.bind("tcp://*:5555");
socket.setsockopt(ZMQ_CONFLATE, 1);
std::cout << "Server Up and Sending\n";
while(true)
{
std::string str = "Hello from C++!";
zmq::message_t msg(str.data(), str.length());
bool ret = socket.send(msg);
if(ret)
{
std::cout << "Sending\n";
}
}
return 0;
}

我正在尝试弄清楚如何在 JeroMQ(Java ZMQ)中接收;这是我目前所拥有的:

  ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.SUB);
socket.connect("tcp://localhost:5555");
socket.subscribe("".getBytes());
while(true)
{
String msg = socket.recvStr();
System.out.println(msg);
}

同样在 PyZMQ(Python ZMQ)中:

import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
address = 'tcp://localhost:5555'
socket.connect(address)
socket.setsockopt_string(zmq.SUBSCRIBE, unicode(''))

print "start"
print "connecting to ", address

while True:
try:
msg = socket.recv_string()
print msg

但是,我的 JeroMQ 尝试或 PyZMQ 尝试都没有收到我的字符串消息。

最佳答案

尝试使用此代码段订阅 :

import zmq
import time

port = "5555"

context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.setsockopt(zmq.SUBSCRIBE, '')
socket.setsockopt(zmq.CONFLATE, 1) # last msg only.
socket.connect("tcp://localhost:%s" % port) # must be placed after above options.

while 1:
time.sleep(1)
data = socket.recv()
print data

[注意]:

如果不起作用,请在您的 C++ ZeroMQ 发布者中删除以下行:

socket.setsockopt(ZMQ_CONFLATE, 1);

关于java - ZeroMQ 跨语言消息翻译 CPPZMQ -- JeroMQ -- PyZMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44836564/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com