0

I am using zeromq to read data from an application which uses msgpack for serializing. The code compiles well but throws an invalid argument error when run. Where am I being wrong.

Here is the error: terminate called after throwing an instance of 'zmq::error_t'

what(): Invalid argument Abort (core dumped)

Here's the code.

#include <zmq.hpp>
#include <iostream>
#include <sstream>
#include <msgpack.hpp>
#include <string>

int main(int argc, char *argv[]){
zmq::context_t context (1);

// Open a req port to talk to application
std::string addr = "tcp://127.0.0.1";
std::string req_port = "55555";
zmq::socket_t req (context, ZMQ_REQ);
req.connect(addr+req_port);

// Ask for the subport
zmq::message_t subPortRequest (8);
memcpy (subPortRequest.data(), "SUB_PORT", 8);
req.send(subPortRequest);

zmq::message_t reply;
req.recv(&reply);

std::string sub_port = std::string(static_cast<char*>(reply.data()), reply.size());
std::cout << sub_port << std::endl;


//  Open a sub port to listen to application
zmq::socket_t sub (context, ZMQ_SUB);
std::cout << addr+sub_port << std::endl;
sub.connect(addr+sub_port);

// subscriptions to everything
sub.setsockopt(ZMQ_SUBSCRIBE, "", strlen(""));

while(1){
    zmq::message_t reply_topic;
    sub.recv(&reply_topic);
    std::string topic = std::string(static_cast<char*>(reply_topic.data()), reply_topic.size());

    zmq::message_t reply_msg;
    sub.recv(&reply_msg);
    std::string msg = std::string(static_cast<char*>(reply_msg.data()), reply_msg.size());

    msgpack::object_handle oh = msgpack::unpack(msg.data(), msg.size());
    msgpack::object obj = oh.get();
    std::cout << obj << std::endl;

} 

}
user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    Where does it crash? Use a debugger to find out. Then look at the function parameters and the manual, what could be wrong ... This way, there are too many unknowns to really help you. – Rene Mar 02 '17 at 16:20

1 Answers1

1

Most probably the string fails to meet the spec:

While the source instructs to do this:

zmq::socket_t req ( context, ZMQ_REQ );  // __________.SET [REQ] access point
// Open a req port to talk to application ____________.SET strings
std::string addr     = "tcp://127.0.0.1"; // _________.SET    "IP"-part
std::string req_port = "55555";           // _________.SET "PORT#"-part

req.connect( addr + req_port );           // _________.CONNECT( "IP"+"PORT#" )

the ZeroMQ .connect() method ought get a string of about this shape:

.connect( "tcp://127.0.0.1:55555" );
------------------------------------------------^


Anyway, enjoy building the Smart Distributed Systems with the powers of ZeroMQ

user3666197
  • 1
  • 6
  • 50
  • 92