[zeromq-dev] epgm / pgm / auto discovery in C++ on Windows and VS2013
James Chapman
james at linux-101.org
Thu Nov 27 11:31:36 CET 2014
On 26 November 2014 at 23:30, Dorvin <dorvin at tlen.pl> wrote:
> I don't see subscription in your code. You have to subscribe to
> something before you receive any message. Also bear in mind bind and
> connect are not always interchangable and in general I would advice
> publisher side to bind.
>
> Jarek
Thanks Jarek
Taking on board your comments I've created a separate project in order
to try and get this mechanism working, it also has the benefit of
being compilable by anyone on the list.
I looked at the following example from the user guide:
http://zguide.zeromq.org/cpp:wuproxy and tried to implement a similar
example. Obviously I'm still doing something wrong because I get
exceptions thrown when trying to bind on the publisher and an
exception thrown when trying to connect on the subscriber.
If I change from epgm to tcp (see publishAddress / subscribeAddress
string) then the publisher sucessfully sends messages, but the
subscriber doesn't receives them.
I'm stumped.
Again, code below. Any help would be appreciated.
-------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "zmq.hpp"
std::string eth0IP = "192.168.0.15";
/**
* Publisher example
*/
int main()
{
// Set up the connection
zmq::context_t context(1);
zmq::socket_t * socket = new zmq::socket_t(context, ZMQ_PUB);
//std::string publishAddress = "epgm://" + eth0IP + ";239.192.1.1:5555";
std::string publishAddress = "tcp://*:5555";
socket->bind(publishAddress.c_str());
//int linger = 0;
//socket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
// Send the message
while (1)
{
std::cout << "Sending Heartbeat Message" << std::endl;
std::string szMessageData = "Foo";
zmq::message_t zmqMsgOut(sizeof(szMessageData) + 1);
memcpy((void *)zmqMsgOut.data(), szMessageData.c_str(),
sizeof(szMessageData));
bool status = socket->send(zmqMsgOut);
if (status) {
std::cout << "Heartbeat Message Sent." << std::endl;
}
else {
std::cerr << "Heartbeat Message NOT Sent." << std::endl;
}
Sleep(1000);
}
// Close and delete the socket
socket->disconnect(publishAddress.c_str());
socket->close();
delete socket;
return 0;
}
-------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "zmq.hpp"
std::string eth0IP = "192.168.0.15";
/**
* Subscriber example
*/
int main()
{
zmq::context_t zmqContext(1);
zmq::socket_t * pZmqSocket = new zmq::socket_t(zmqContext, ZMQ_SUB);
//std::string subscribeAddress = "epgm://" + eth0IP + ";239.192.1.1:5555";
std::string subscribeAddress = "tcp://localhost:5556";
pZmqSocket->connect(subscribeAddress.c_str());
pZmqSocket->setsockopt(ZMQ_SUBSCRIBE, "", 0);
std::cout << "Heartbeat responder BOUND" << std::endl;
while (1)
{
std::cout << ".";
//pZmqSocket->connected();
zmq::message_t * pZmqMsgIn = new zmq::message_t();
bool success = pZmqSocket->recv(pZmqMsgIn);
if (success) {
std::cout << "======== MESSAGE RECEIVED =========" << std::endl;
}
else {
std::cerr << "Error receiving message" << std::endl;
}
//std::string * szMessageData = static_cast<std::string
*>(pZmqMsgIn->data());
delete pZmqMsgIn;
}
delete pZmqSocket;
return 0;
}
More information about the zeromq-dev
mailing list