-2

I have a pub/sub system composed by two VMs (VirtualBOx, Ubuntu 18.04 and python-zmq[16.0.2-2build2]) running within the same physical machine (Win10). Both machines are configured as Bridge.

The subscriber can receive the publisher messages if it is configured with the pub IP, however, it is not scalable with multiple publishers.

PUB (server) is configured as:

    import zmq
    import random
    import sys
    import time
    
    port = "5557"
    if len(sys.argv) > 1:
        port =  sys.argv[1]
        int(port)
    
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://*:%s" % port)
    while True:
        topic = random.randrange(9999,10005)
        messagedata = random.randrange(1,215) - 80
        print "%d %d" % (topic, messagedata)
        socket.send("%d %d" % (topic, messagedata))
        time.sleep(1)

And the SUB (client) as

    import sys
    import zmq
    
    port = "5557"
    if len(sys.argv) > 1:
        port =  sys.argv[1]
        int(port)
        
    if len(sys.argv) > 2:
        port1 =  sys.argv[2]
        int(port1)
    
    # Socket to talk to server
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    print "Collecting updates from weather server..."
    socket.connect ("tcp://192.168.1.66:%s" % port)
    
    if len(sys.argv) > 2:
        socket.connect ("tcp://localhost:%s" % port1)
    # Subscribe to zipcode, default is NYC, 10001
    topicfilter = "10001"
    socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
    
    # Process 5 updates
    total_value = 0
    for update_nbr in range (5):
        string = socket.recv()
        topic, messagedata = string.split()
        total_value += int(messagedata)
        print topic, messagedata

    print "Average messagedata value for topic '%s' was %dF" % (topicfilter, total_value / update_nbr)

I've tried to configure at client in socket.connect ("tcp://IP:%s" % port):

"*"

Gives the error:


Traceback (most recent call last):
  File "sub_client.py", line 18, in <module>
    socket.connect ("tcp://*:%s" % port)
  File "zmq/backend/cython/socket.pyx", line 528, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:5980)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:8400)
zmq.error.ZMQError: Invalid argument
192.168.1.1 (GW), 192.168.1.255 (broadcast), localhost/127.0.0.1 and it's IP (192.168.1.55) -> does not receive messages

192.168.1.66 (server's IP) -> Does receive messages but not practical in a large scale system

Any way to solve this?

ewokx
  • 2,204
  • 3
  • 14
  • 27
pepe pep
  • 27
  • 5
  • 1
    Welcome to SO. Please clarify how the IPs are set. – ewokx Nov 20 '20 at 23:12
  • 1
    For the required [mcve], please hardcode the port numbers. Also, I'm wonderin if this works when it all runs on one machine. BTW, as a new user, take the [tour] and read [ask]. – Ulrich Eckhardt Nov 21 '20 at 11:39
  • Thx @ewong. The IPs are set automatically by DHCP (VMs are in bridged mode). This is the MRE, what do you mean to hardcore the port numbers, they are within the code. It works when they are at the same machine using "localhost" in both PUB/SUB. – pepe pep Nov 23 '20 at 10:27

1 Answers1

0

ZMQ/ZeroMQ is not MQTT so the ZMQ library won't be able to connect to a MQTT broker.

hardillb
  • 54,545
  • 11
  • 67
  • 105