Reputation: 1239
I am trying to use broadcasting packets to subnet address, I successfully tried to do that using socket's broadcast option, But I recently started learning ZeroMQ so I would like to use it to broadcast the packets to the subnet. I used zmq.PUB
, zmq.SUB
but at the subscriber side, the packets are undelivered because I use subnet address. If I use IP address of the machine then it works, but that's not what I want.
Is there any option for broadcasting using ZMQ?
Here is the code I tried so far:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://192.X.X.255:9999") # 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.x.x.255:9999') -> publishing only to subnet
while True:
print(sub.recv())
We can do the broadcasting of packets using a regular socket, for example, using:
sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
But I want to replace this in a way I do it with ZMQ. Does the ZMQ really have a broadcast discovery in a different way or we should use the same code above as we do for regular broadcasting?
Upvotes: 0
Views: 897
Reputation: 34006
Suppose you have three Machines (M1, M2, M3) with three different IP addresses with the same subnet and a defined port. We want to publish a message (from M1) to each subscribers (M1, M2), therefore we would have the following code snippet:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:9999") # Note.
while True:
socket.send_string('hello-all')
time.sleep(1)
context = zmq.Context()
sub=context.socket(zmq.SUB)
sub.setsockopt(zmq.SUBSCRIBE, b"")
sub.connect('tcp://Machine1_IP:9999') # Note
while True:
print(sub.recv())
context = zmq.Context()
sub=context.socket(zmq.SUB)
sub.setsockopt(zmq.SUBSCRIBE, b"")
sub.connect('tcp://Machine1_IP:9999') # Note
while True:
print(sub.recv())
Upvotes: 1