Hi Bernardo,

                The http://zguide.zeromq.org/page:all tells us;

“The ROUTER socket, unlike other sockets, tracks every connection it has, and 
tells the caller about these. The way it tells the caller is to stick the 
connection identity in front of each message received. An identity, sometimes 
called an address, is just a binary string with no meaning except "this is a 
unique handle to the connection". Then, when you send a message via a ROUTER 
socket, you first send an identity frame.
The zmq_socket()<http://api.zeromq.org/3-2:zmq_socket> man page describes it 
thus:
When receiving messages a ZMQ_ROUTER socket shall prepend a message part 
containing the identity of the originating peer to the message before passing 
it to the application. Messages received are fair-queued from among all 
connected peers. When sending messages a ZMQ_ROUTER socket shall remove the 
first part of the message and use it to determine the identity of the peer the 
message shall be routed to.”
Though I haven’t run your code I suspect that your ROUTER will bounce back your 
test message to the originating DEALER?  This would be because the identity 
frame was prepended by the ROUTER on receipt and the message is sent back 
through the ROUTER; where it strips off the identity and uses the value to 
route the message back to the relevant DEALER.
In order to send it to a different destination it is necessary to have the 
‘identity’ frame of that other DEALER and to prepend that identity when you 
send the message; otherwise the ROUTER won’t know where to deliver the message.
When receiving off a ROUTER socket the ‘identity’ is the first frame of the 
message.  You could capture it and store it. Then later prepend it as the first 
frame of a message you wish to send to a particular DEALER via your ROUTER.
HTH,
Stephen


From: zeromq-dev [mailto:[email protected]] On Behalf Of 
Bernardo Augusto García Loaiza
Sent: 14 March 2018 23:17
To: ZeroMQ development list
Subject: [zeromq-dev] ZMQ DEALER - ROUTER Communication


I am currently working on a project that requires some communication over the 
network of a different data types from some entities of a distributed system 
and I am using ZMQ.

The main goal of the project is to have a central node which services clients 
which can connect at any time. For each client connected, the central node 
should manage the message communication between the two.
Currently, and by the moment, all communication is happening over TCP.

The clients need to send and receive messages at any time so they are 
ZMQ_DEALER type sockets and the central node is ZMQ_ROUTER

I have been reading, about of the different patterns communication and I think 
the Asynchronous Client/Server 
pattern<http://zguide.zeromq.org/page:all#The-Asynchronous-Client-Server-Pattern>
 is suited because I am interested in having several clients talking to each 
other in a collaborative way, having maybe a server broker or middleware, which 
be useful to receive and forward messages between entities related and because 
basically, the DEALER - TO - 
ROUTER<http://zguide.zeromq.org/php:chapter3#The-DEALER-to-ROUTER-Combination> 
combination is that I need.

[cid:[email protected]]

I have a ZMQ_DEALER socket client which connect to ZMQ_ROUTER socket server

#include <zmq.hpp>

#include "zhelpers.hpp"

using namespace std;



int main(int argc, char *argv[])

{



    zmq::context_t context(1);

    zmq::socket_t client(context, ZMQ_DEALER);



    const string endpoint = "tcp://localhost:5559";



    client.setsockopt(ZMQ_IDENTITY, "PEER1", 5);

    cout << "Connecting to ZMQ Network Manager " << endpoint << "..." << endl;

    client.connect(endpoint);

    for (int request = 0; request < 10; request++)

    {



        s_sendmore(client, "");

        s_send(client, "Testing sending some data");



        std::string string = s_recv(client);



        std::cout << "Received reply " << request

                  << " [" << string << "]" << std::endl;

    }

}

On my server code, I have a ZMQ_ROUTER which receive and manage the messages 
is, making bind it to a well port. This server is made in Python

import zmq

context = zmq.Context()

frontend = context.socket(zmq.ROUTER)

frontend.bind("tcp://*:5559")



# Initialize a poll set

poller = zmq.Poller()

poller.register(frontend, zmq.POLLIN)



print("Creating Server Network Manager Router")



while True:

    socks = dict(poller.poll())



    if socks.get(frontend) == zmq.POLLIN:

        message = frontend.recv_multipart()

        print(message)

        frontend.send_multipart(message)

On my other peer/client I have the following:


#include <zmq.hpp>

#include "zhelpers.hpp"

using namespace std;



int main (int argc, char *argv[])

{



    zmq::context_t context(1);

    zmq::socket_t peer2(context, ZMQ_DEALER);



    const string endpoint = "tcp://localhost:5559";



    peer2.setsockopt(ZMQ_IDENTITY, "PEER2", 5);

    cout << "Connecting to ZMQ Network Manager " << endpoint << "..." << endl;

    peer2.connect(endpoint);

    //s_sendmore(peer2, "");

    //s_send(peer2, "Probando");



    //std::string string = s_recv(peer2);



    //std::cout << "Received reply " << " [" << string << "]" << std::endl;



    for (int request = 0; request < 10; request++)

    {



        s_sendmore(peer2, "");

        s_send(peer2, "Probando");



        std::string string = s_recv(peer2);



        std::cout << "Received reply " << request

                  << " [" << string << "]" << std::endl;

    }



}


But each that I execute some client, their respective messages do not arrive at 
another peer client. The messages arrive at ZMQ_ROUTER, but I haven't clear on 
my server code how to should I forward this messages to the respective clients.

I think so (may be obvious) that in my server code, I am not sending messages.



Bernardo Augusto García Loaiza
Ingeniero de Sistemas
Estudiante de Maestría en Ingeniería Informática - Universidad EAFIT
http://about.me/bgarcial

_______________________________________________
zeromq-dev mailing list
[email protected]
https://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to