[英]Subscriber does not receive message, Pyzmq
I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received.我正在尝试使用已知端口将消息(就像使用原始套接字时的广播)发布到我的子网,但在订阅者端,没有收到消息。 The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP.这个想法是第二台机器不应该知道第一台机器的IP地址,这就是我使用广播IP的原因。 With UDP or TCP raw socket, it works but I am trying to learn pub-sub
pattern not sure how to incorporate that idea.使用 UDP 或 TCP 原始套接字,它可以工作,但我正在尝试学习pub-sub
模式,不确定如何结合这个想法。
This is my codes:这是我的代码:
Publisher:出版商:
import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)
Subscriber:订户:
context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())
In terms of raw UDP, I wrote a code which works perfectly.在原始 UDP 方面,我编写了一个完美运行的代码。
broadcast:播送:
def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)
sock.sendto(msg.encode(), (Host,port))
recv:接收:
def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)
It seems you forgot the zmq.SUB
in the subscriber side.您似乎忘记了订阅者端的zmq.SUB
。 Also you used sub.setsocketopt()
instead of sub.setsockopt()
.您还使用了sub.setsocketopt()
而不是sub.setsockopt()
。
Try it:尝试一下:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[ NOTE ]: [注意]:
.bind()
and .connect()
in subscriber and publisher with your policy.您还可以使用您的策略更改订阅者和发布者中的.bind()
和.connect()
。 ( This post is relevant). ( 这篇文章是相关的)。5677
is open in the firewall.确保5677
在防火墙中是打开的。socket.bind("tcp://*:5677")
or socket.bind("tcp://0.0.0.0:5677")
is broadcasting trick. socket.bind("tcp://*:5677")
或socket.bind("tcp://0.0.0.0:5677")
是广播技巧。I think the problem is that the SUB
socket cannot register itself with the PUB
socket.我认为问题在于SUB
套接字无法向PUB
套接字注册自己。 Even though in-concept the data only goes from PUB
to SUB
, in reality, there are also control messages (eg subscription topics), being sent back to the PUB
.尽管在概念中数据仅从PUB
到SUB
,但实际上,也有控制消息(例如订阅主题)被发送回PUB
。
If your netmask is 255.255.255.0, this will probably not work as expected.如果您的网络掩码是 255.255.255.0,这可能不会按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.