[zeromq-dev] epgm / pgm / auto discovery in C++ on Windows and VS2013

James Chapman james at linux-101.org
Wed Nov 26 23:06:44 CET 2014


Hello ZeroMQ mailing list

I hoping someone can give me some pointers as I'm struggling with auto discovery

I'm trying to build an auto discovery mechanism whereby a server
broadcasts messages via multicast or broadcast address to which
clients will respond.

For now I'm just trying to send a message from A and receive it on B.
I should also mention that I'm trying to send and receive from the
same machine.

I've tried epgm, pgm, tcp, udp, broadcast addresses, multicast
addresses, I just don't seem to be making any headway, so any guidance
would be greatly appreciated. Where am I going wrong and has anyone
else tackled this problem on the Windows platform before?

I've copied code from the relevant sections in each module and pasted it below.

Any help would be appreciated.

Thanks in advance.

James



/**
* Server sending code:
*/
void MessageSender::sendHeartbeat()
{
    DEBUG_LOG("MessageSender::sendHeartbeat()");
    // Set up the connection
    zmq::context_t context(1);
    zmq::socket_t * socket = new zmq::socket_t(context, ZMQ_PUB);
    //std::string conIp = "epgm://192.168.0.15;239.192.1.1:5555";
    //std::string conIp = "tcp://192.168.0.255:6669";
    //std::string conIp = "tcp://255.255.255.255:6669";
    std::string conIp("tcp://192.168.0.255:6669");
    socket->connect(conIp.c_str());
    int linger = 0;
    socket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
    DEBUG_LOG("Sending Heartbeat Message");

    // Send the message
    {
        Phoenix::Common::Message * pMessageOut = new Phoenix::Common::Message();
        pMessageOut->header.messageType = Phoenix::Common::MessageType::PING;

        zmq::message_t zmqMsgOut(sizeof(*pMessageOut) + 1);
        memcpy((void *)zmqMsgOut.data(), pMessageOut, sizeof(*pMessageOut));
        bool status = socket->send(zmqMsgOut);
        DEBUG_LOG("Heartbeat Message Sent.");
    }

    // Close and delete the socket
    socket->disconnect(conIp.c_str());
    socket->close();
    delete socket;
}


/**
* Client receiving code
*/
void HealthSlave::hearbeatResponder()
{
    DEBUG_LOG("HealthSlave::hearbeatResponder()");
    zmq::context_t zmqContext(1);
    zmq::socket_t * pZmqSocket = new zmq::socket_t(zmqContext, ZMQ_SUB);
    //std::string listenString = "epgm://192.168.0.15;239.192.1.1:5555";
    //std::string listenString = "tcp://192.168.0.255:6669";
    //std::string listenString = "tcp://255.255.255.255:6669";
    std::string listenString = "tcp://*:6669";
    pZmqSocket->bind(listenString.c_str());
    DEBUG_LOG("Heartbeat responder BOUND");
    while (1)
    {

        pZmqSocket->connected();
        zmq::message_t * pZmqMsgIn = new zmq::message_t();

        try
        {
            pZmqSocket->recv(pZmqMsgIn);
        }
        catch (std::exception& e)
        {
            std::stringstream streamErrMsg;
            streamErrMsg << "Exception caught during socket.recv(): "
<< e.what() << std::endl;
            ERROR_OUT(streamErrMsg.str());
        }

        Phoenix::Common::Message * pRpcMessageIn =
static_cast<Phoenix::Common::Message *>(pZmqMsgIn->data());
        //HealthSlave::messageHandler(pZmqSocket, pRpcMessageIn);

        INFO_OUT("============= HEARTBEAT RECEIVED ==============");

        delete pZmqMsgIn;

    }
    delete pZmqSocket;
}



More information about the zeromq-dev mailing list