peer.py
import zmq
import time
if __name__ == '__main__':
context = zmq.Context()
socket = context.socket(zmq.PUB)
while True:
print "I: Publishing"
socket.bind("tcp://*:5555")
socket.send_multipart(['general', 'Unique peer information'])
socket.unbind("tcp://*:5555")
time.sleep(1)
scanner.py
import zmq
if __name__ == '__main__':
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, 'general')
socket.connect("tcp://localhost:5555")
print "I: Scanning 5555"
while True:
message = socket.recv_multipart()
print "I: Receiving: {}".format(message)
I'm attempting to broadcast multiple peers over the same port, on the same computer and have a single "scanner" listen in and "see" who is available. Each peer would broadcast its contact information, a client would then use the scanner to find out who is available and then use the broadcasted information to connect via a REQ/REP channel.
To make this work, I've attempted to quickly bind a PUB socket, broadcast information about a peer, and then unbind so as to let other peers bind to the same socket, at a different time, and broadcast a different set of identifiers for the next peer.
I'm suspecting that messages get discarded before getting sent due to the unbinding (the same occurs with a call to close()) but I can't figure out how to get it to empty the queue prior to closing the connection so as to not discard any messages.
Any ideas?
- Windows 8.1 x64
- Python 2.7.7 x64
- PyZMQ 4.0.4
Edit
I asked the same question on the ZMQ mailing-list and got some interesting replies, see here: http://lists.zeromq.org/pipermail/zeromq-dev/2014-June/026444.html