[zeromq-dev] router/dealer doubts

andrea crotti andrea.crotti.0 at gmail.com
Tue Aug 21 12:53:59 CEST 2012


I have some troubles understanding the ROUTER/DEALER sockets..

So first of all let's see if I understood correctly..
ROUTER/DEALER is used for extending REQ/REP, where in other words, if
I want to have:
- 1 client and multiple server, I add a dealer in between
- 1 server and multiple clients, I add a router in between

If I want to have multiple servers and clients than I add both, is that correct?

Then the ROUTER/DEALER add/strip the identity of the sender/receiver
from every message, right?
But should I handle this myself or is not necessary?

About this in one of the examples in the guide I found:

client = context.socket(zmq.REQ)
client.identity = 'name'

but everywhere else I see:
worker.setsockopt(zmq.IDENTITY, "WORKER")

are they equivalent?

I was then trying to create a simple program to spawn multiple
clients, multiple servers, one router and one dealer, as below:


The problem is that I get some strange messages like these:

('received', '\x00GM\xc1\x17`]C\xa0\xaeTDc\xdf\x8f\xc7\x8c')
('received', '')
('received', 'Task?')

What is it and where does that come from?


from multiprocessing import Process
from time import sleep

import zmq

FRONT_PORT = 'tcp://127.0.0.1:5555'
BACK_PORT = 'tcp://127.0.0.1:5556'



def start_router():
    """A router allows to connect multiple clients to the same server,
    while a dealer allows to have multiple servers
    """
    context = zmq.Context()
    frontend = context.socket(zmq.ROUTER)
    frontend.bind(FRONT_PORT)

    backend = context.socket(zmq.DEALER)
    backend.bind(BACK_PORT)

    poll = zmq.Poller()
    poll.register(frontend, zmq.POLLIN)
    poll.register(backend, zmq.POLLIN)

    while True:
        socks = dict(poll.poll())
        if socks.get(frontend) == zmq.POLLIN:
            msg = frontend.recv()
            print("received", msg)
            backend.send(msg)

        if socks.get(backend) == zmq.POLLIN:
            msg = backend.recv()
            print("received", msg)
            frontend.send(msg)


def start_server(idx):
    context = zmq.Context()
    tasker = context.socket(zmq.REP)
    tasker.connect(BACK_PORT)
    while True:
        sleep(1)
        question = tasker.recv()
        print("Got question %s" % question)

        if question == 'Task?':
            tasker.send('Hello world')


def start_client(idx):
    context = zmq.Context()
    worker = context.socket(zmq.REQ)
    worker.connect(FRONT_PORT)
    worker.setsockopt(zmq.IDENTITY, str(idx))

    while True:
        worker.send('Task?')
        ans = worker.recv()
        print("client %d received answer %s" % (idx, ans))


if __name__ == '__main__':
    Process(target=start_router).start()
    for i in range(4):
        Process(target=start_client, args=(i, )).start()

    for i in range(3):
        Process(target=start_server, args=(i, )).start()



More information about the zeromq-dev mailing list