Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ C# client not receiving c++ Server Publish/Subscribe Versions 3.2

Tags:

c++

c#

zeromq

i have problems getting a c# zeromq client to receive messages from a c++ server using the Subscribe Publish pattern.

The server setup is one transport layer with two types of transport strategies (PublishSubsribe and RequestReply). These are coded in c++ and use the 3.2.0 libzmq.

class PublishSubsribe : public ITransportStrategy
{
private:
     zmq::context_t _context;
     zmq::socket_t _socket;

public:

    PublishSubsribe(std::string url)
        :
    _context(1),
    _socket(_context, ZMQ_PUB)
    {
        std::cout << "Binding  " << url << std::endl;
        _socket.bind(url.c_str());
    }

    virtual void Wait()
    {
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }

    virtual void Send(zmq::message_t& message) 
    {
        _socket.send(outMessage) ;
    }
    ..
};  
class RequestReply : public ITransportStrategy
{
private:
     zmq::context_t _context;
     zmq::socket_t _socket;

public:

    RequestReply(std::string url)
        :
    _context(1),
    _socket(_context, ZMQ_REP)
    {
        _socket.bind(url.c_str());
    }

    virtual void Send(zmq::message_t& message) 
    {
        _socket.send(message);
    }

    virtual void Wait()
    {
        zmq_msg_t request;
        zmq_msg_init (&request);
        zmq_msg_recv (&request, _socket, 0);
        zmq_msg_close (&request);
    }
    ..
};

The Request/Reply pattern works when the clients are either c#/c++. Leading me to think that there is no encoding issue... might be wrong though. Furthermore, i have a client subscriber in c++ that does recieve the messages form the Publisher.

So in short, my c# client in PubSub

//Bundled libzmq version: **3.2.2-rc2**  

        using (ZmqContext context = ZmqContext.Create())
        using (ZmqSocket client = context.CreateSocket(SocketType.SUB))
        {
            client.Connect("tcp://192.168.2.12:5555");

            client.SubscribeAll();
            while (true)
            {
                var msg = client.Receive(Encoding.UTF8);
                Console.WriteLine("Received Pub/Sub: yes");

            }
        }

is not receiving any messages, and i've tried all encodings, eventhough the UTF8 works for the ReqRep.

like image 811
user1875444 Avatar asked Nov 04 '22 09:11

user1875444


1 Answers

You mention you are not using the same version of libzmq in the C# and C++ project, might it be the problem? I had the same issue when unwittingly using libzmq 2.x and 3.x

Also is there any synchronization code to ensure you publish messages after the subscriber connects (see "slow joiner syndrome" in the ZMQ Guide) ?
like image 112
JJ15k Avatar answered Nov 12 '22 21:11

JJ15k