I am trying to set up a basic communication system between client and server using ZMQ. I am using protobuf for the message format.
My problem is when I send the message from client the message size is 34 but the message size received on the server is 0.
Following is my code;
Client.cpp
tutorial::Person person;
person.set_id(1234);
person.set_name("john");
person.set_email("john#mxyz.com");
person.set_phonenumber("12345678");
zmq::context_t context (1); // Prepare our context and socket
zmq::socket_t socket (context, ZMQ_PAIR);
std::cout << "Connecting to server…" << std::endl;
int linger = 0;// ms
socket.setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
socket.connect ("tcp://127.0.0.1:20000");
std::string msg_str;
person.SerializeToString(&msg_str);
std::cout << "Size of message string is "<< msg_str.size()<<std::endl;
zmq::message_t request (msg_str.size());
memcpy ((void *) request.data (), msg_str.c_str(), msg_str.size());
std::cout << "Sending Person data ..." << std::endl;
socket.send (request);
socket.close();
google::protobuf::ShutdownProtobufLibrary();
return 0;
Server.cpp :
zmq::context_t context(1); // Prepare our context and socket
zmq::socket_t socket(context, ZMQ_PAIR);
int linger = 0; // ms
socket.setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
socket.bind("tcp://127.0.0.1:20000");
while (true)
{
zmq::message_t request;
int recieved = socket.recv(&request);
std::string msg(static_cast<char*>(request.data()),request.size());
std::cout<<"Size of message recieved is "<< msg.size()<<std::endl;
tutorial::Person person;
person.ParseFromString(msg);
std::string text_str1;
google::protobuf::TextFormat::PrintToString(person, &text_str1);
}
socket.close();
Output of Client is :
Size of message string is 34
Output of server is :
Size of message received is 0
I have tried tried switching to ParseToArray also but it did not help.
Any kind of help is appreciated.
I have solved it with another way. Actually earlier I installed libzmq lib , but later when I switched to libzmq3-dev it worked for me.
I had the exact same problem; what I found is that the zmq::message_t had to go out of scope OR rebuild needed to be called.
For example, if the publisher looked like this.
zmq::message_t msg(3);
memcpy(msg.data(), "abc", 3);
while (1 == 1) {
pubSocket.send(msg);
sleep(1);
}
The subscriber would receive messages of zero length. If the code was changed to
while (1 == 1) {
zmq::message_t msg(3);
memcpy(msg.data(), "abc", 3);
pubSocket.send(msg);
sleep(1);
}
OR
zmq::message_t msg(3);
memcpy(msg.data(), "abc", 3);
while (1 == 1) {
pubSocket.send(msg);
msg.rebuild(3);
sleep(1);
}
Then the subscriber would receive a 3-byte message.
Related
I am building a server and client in c++ which use ZeroMQ PAIR sockets to communicate. Since I want to call some RPC from client to server, I am using JSON::Value structure to encode the function name and arguments, so that the server can parse it and call the appropriate function.
I checked out gRPC for doing the same but I felt it as an overkill as it requires too much effort to get it right.
As a draft, I am created a sample client and server apps. I am able to send the data from client to server, but on the server side I am receiving parse error. Could anyone suggest me what I might be doing wrong?
client.cpp
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <jsoncpp/json/value.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/writer.h>
int main ()
{
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_PAIR);
/**
* Json object value parser
*/
Json::Value out;
Json::Value res;
Json::StreamWriterBuilder builder;
out["MESSAGE"] = "Anaconda";
out["NEWS"] = "Something is wrong";
std::cout << "Connecting to hello world server…" << std::endl;
socket.connect ("tcp://localhost:5555");
zmq::message_t request(out.size());
std::string str = Json::writeString(builder, out);
std::cout<<str<<std::endl;
memcpy (request.data(),&str, out.size());
socket.send (request);
// Get the reply.
zmq::message_t reply;
socket.recv (&reply);
return 0;
}
server.cpp
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_PAIR);
socket.bind ("tcp://*:5555");
/**
* Json object value parser
*/
Json::Reader mReader = {};
Json::Value res;
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::string str = std::string(static_cast<char*>(request.data()), request.size());
std::cout <<str<<std::endl;
auto ok = mReader.parse(str, res);
if (!ok) {
std::cout <<"ConHash:: Error while parsing: %s "<< mReader.getFormattedErrorMessages().c_str() << std::endl;
return false;
} else {
std::cout<<"Successfully parsed !!" <<std::endl;
}
// Do some 'work'
}
return 0;
}
Also could you please advice me is there any other better way to make RPC from client to server?
You are using the size of the Json::Value out object as the size to send over the socket. This is incorrect, you need to use the size of the newly created string.
Change the code to:
std::string str = Json::writeString(builder, out);
zmq::message_t request(str.c_str(), str.size());
socket.send (request);
I try to run simple ZMQ application ( ROUTER/DEALER ).
I just send a request from DEALER to ROUTER, send it back. But DEALER cannot receive it.
I run it in one process ( ROUTER has its own thread ).
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>
void router()
{
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_ROUTER);
socket.bind("tcp://*:5561");
while(1)
{
// Wait for next request from client
zmq::message_t reply;
socket.recv (&reply);
std::cout << "Router: Received request" << std::endl;
// Send reply back to client
std::string string= "example";
zmq::message_t message(string.size());
memcpy (message.data(), string.data(), string.size());
std::cout << "Router: Sending" << std::endl;
socket.send (message);
}
}
int main ()
{
std::thread t{&router};
// Prepare our context and socket
zmq::context_t context (2);
zmq::socket_t socket (context, ZMQ_DEALER);
std::cout << "Dealer: Connecting to hello world server…" << std::endl;
socket.connect ("tcp://127.0.0.1:5561");
for (int i = 0; i != 10; i++)
{
zmq::message_t request (5);
memcpy (request.data (), "Hello", 5);
std::cout << "Dealer: Sending Hello " << i << "…" << std::endl;
socket.send (request);
zmq::message_t reply;
socket.recv (&reply);
std::cout << "Dealer: Received " << i << std::endl;
}
return 0;
}
I have an output:
Dealer: Connecting to hello world server…
Dealer: Sending Hello 0…
Router: Received request
Router: Sending
Router: Received request
Router: Sending
From ZMQ's documentation on socket :
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.
So modify your code to something like this :
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>
#include <unistd.h>
void router()
{
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_ROUTER);
socket.bind("tcp://*:5561");
while(1) // Fix that infinite loop or your thread won't join
{
// Wait for next request from client
zmq::message_t id;
socket.recv (&id);
zmq::message_t reply;
socket.recv (&reply);
std::cout << std::string(static_cast<char*>(reply.data()),reply.size()) << std::endl;
std::cout << "Router: Received request" << std::endl;
// Send reply back to client
zmq::message_t copy_id;
copy_id.copy(&id);
std::string string= "example";
zmq::message_t message(string.size());
memcpy (message.data(), string.data(), string.size());
std::cout << "Router: Sending" << std::endl;
socket.send(id, ZMQ_SNDMORE);
socket.send(message);
}
sleep(1);
socket.setsockopt(ZMQ_LINGER, 0);
socket.close();
context.close();
}
int main ()
{
std::thread t{&router};
// Prepare our context and socket
zmq::context_t context (2);
zmq::socket_t socket (context, ZMQ_DEALER);
std::cout << "Dealer: Connecting to hello world server…" << std::endl;
socket.connect ("tcp://127.0.0.1:5561");
for (int i = 0; i != 10; i++)
{
zmq::message_t request (5);
memcpy (request.data (), "Hello", 5);
std::cout << "Dealer: Sending Hello " << i << "…" << std::endl;
socket.send(request);
zmq::message_t reply;
socket.recv(&reply);
std::cout << "Dealer: Received " << i << std::endl;
}
socket.setsockopt(ZMQ_LINGER, 0);
socket.close();
context.close();
t.join();
return 0;
}
My Requirements:
High throughput, atleast 5000 messages per second
Order of delivery not important
Publisher, as obvious, should not wait for a response and should not care if a Subscriber is listening or not
Background:
I am creating a new thread for every message because if I dont, the messages generation part will out-speed the sending thread and messages get lost, so a thread for each message seems to be the right approach
Problem:
The problem is that somehow the threads that are started to send out the zMQ message are not being terminated (not exiting/finishing). There seems to be a problem in the following line:
s_send(*client, request.str());
because if I remove it then the threads terminate fine, so probably its this line which is causing problems, my first guess was that the thread is waiting for a response, but does a zmq_PUB wait for a response?
Here is my code:
void *SendHello(void *threadid) {
long tid;
tid = (long) threadid;
//cout << "Hello World! Thread ID, " << tid << endl;
std::stringstream request;
//writing the hex as request to be sent to the server
request << tid;
s_send(*client, request.str());
pthread_exit(NULL);
}
int main() {
int sequence = 0;
int NUM_THREADS = 1000;
while (1) {
pthread_t threads[NUM_THREADS];
int rc;
int i;
for (i = 0; i < NUM_THREADS; i++) {
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, SendHello, (void *) i);
pthread_detach(threads[i]);
sched_yield();
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
//usleep(1000);
sleep(1);
}
pthread_exit(NULL);
//delete client;
return 0;
}
My Question:
Do I need to tweak zMQ sockets so that the PUB doesnt wait for a reply what am I doing wrong?
Edit:
Adding client definition:
static zmq::socket_t * s_client_socket(zmq::context_t & context) {
std::cout << "I: connecting to server." << std::endl;
zmq::socket_t * client = new zmq::socket_t(context, ZMQ_SUB);
client->connect("tcp://localhost:5555");
// Configure socket to not wait at close time
int linger = 0;
client->setsockopt(ZMQ_LINGER, &linger, sizeof (linger));
return client;
}
zmq::context_t context(1);
zmq::socket_t * client = s_client_socket(context);
but does a zmq_PUB wait for a response?
No, this could be the case if your socket wasn't a PUB socket and you hit the high-water mark, but this isn't the case. Do the messages get sent?
I am a new-by to ZeroMQ and make my way through the C++ hello-world example of the echo client-server pattern (Request-Reply). The server looks like:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
Now my question: How can I access / read the real data that socket.recv() ? Trying:
std::cout << request << std::endl;
resulted in an error message:
error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits =
std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)
(& std::cout)), ((const char*)"Received Hello")) << request’
The same goes for the client side that is sending the message. I don't find a way to display the real message...
The hello world example goes only half way and outputs the hard-coded values:
std::cout << "Received Hello" << std::endl;
Printing the actual response can be done as follows:
zmq::message_t reply;
socket.recv (&reply);
std::string rpl = std::string(static_cast<char*>(reply.data()), reply.size());
std::cout << rpl << std::endl;
There are some other useful examples in zhelpers.hpp.
I found that the following does what I want:
zmq::message_t request (msglen);
memcpy ( (void *) request.data(), myMessage, msglen);
char * requestmsg = new char [msglen];
memcpy (requestmsg, request.data(), request.size());
requestsocket.send (request);
std::cout << "Sending " << requestmsg << std::endl;
where msglen is of type int and myMessage is const char * tyoe.
In this way, the server receives a human readable message.
Hope this is not against any zeromq rules...
While I think we need to go through the guide before we can write elegant ZeroMQ codes. I found lines of simple codes from the HELLO WORLD example for extracting data received from the socket and sending back response:
zmq::message_t request;
socket.recv (&request);
std::cout << "Received request: [" << (char*) request.data() << "]" << std::endl;
// Do some 'work'
Sleep (1);
// Send reply back to client
zmq::message_t reply (6);
memcpy ((void *) reply.data (), "World", 6);
socket.send (reply);
However, this solution does not specify the length of received data, following Nikolai Koudelia's way above, I make a string for received data:
std::cout << "Received request: [" << std::string(static_cast<char*>(request.data()), request.size()) << "]" << std::endl;
I can't get ZeroMQ C++ wrapper to receive multipart messages. The same code using C version works just fine, but it leads to an exception with no explanations at all with C++. The multipart handling code is as follows:
int _tmain(int argc, _TCHAR* argv[])
{
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://*:5555");
while(true) {
// the following two lines lead to exception
zmq::message_t request;
socket.recv(&request);
//zmq_msg_t message;
//zmq_msg_init (&message);
//zmq_recv (socket, &message, 0);
}
return 0;
}
It is extremely simple; this version does not work. but if I comment out the first two lines in the while loop and uncomment the currently commented (C version) code, it works.
This is Windows XP sp3, Zeromq 2.1.1 and Visual Studio 2010 Express.
If I send single part messages, both versions work fine.
What am I doing wrong?
I'm also a newbie in ZMQ and I too had to struggle a lot in order to understand multipart messaging using REP/REQ in ZeroMQ. I had to go through multiple resources and stitch data in order to understand this. I think this answer will help many seekers in the near future that's why I am sharing the client and server code here. I have tested this code and it is working perfectly fine. However, being a newbie there are chances that I would have missed something vital. Please share your valuable inputs.
Server Code
void
serverMultipartREPREQ()
{
try
{
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://*:5556");
std::cout << "Listening at port 5556..." << std::endl;
zmq::message_t reply;
socket.recv(reply, zmq::recv_flags::none);
auto rep = std::string(static_cast<char*> (reply.data()), reply.size());
std::cout << "Received: " << rep << std::endl;
while(1)
{
if (input == "exit")
break;
for (int j = 0; j < 3; ++j)
{
std::string s("Message no - " + std::to_string(j));
zmq::message_t message(s.length());
memcpy(message.data(), s.c_str(), s.length());
std::cout << "Sending: " << s << std::endl;
if (j != 2)
socket.send(message, zmq::send_flags::sndmore);
else
socket.send(message, zmq::send_flags::none);
}
}
}
catch (const zmq::error_t& ze)
{
std::cout << "Exception: " << ze.what() << std::endl;
}
Sleep(5000);
}
Client code
void
clientMultipartREQREP()
{
try
{
zmq::context_t context(1);
std::cout << "Connecting to socket at 5556" << std::endl;
zmq::socket_t socket(context, ZMQ_REQ);
socket.connect("tcp://localhost:5556");
std::cout << "Connected to socket at 5556" << std::endl;
std::string msg("Hii this is client...");
zmq::message_t message(msg.length());
memcpy(message.data(), msg.c_str(), msg.length());
socket.send(message, zmq::send_flags::none); // send to server (request message)
while (true)
{
__int64 more = 1;
if (more)
{
zmq::message_t message;
socket.recv(message, zmq::recv_flags::none);
auto rep = std::string(static_cast<char*> (message.data()), message.size());
std::cout << "Reading from client: " << rep << std::endl;
size_t size = sizeof(__int64);
socket.getsockopt(ZMQ_RCVMORE, &more, &size); // if msg is not the last one then more = 1 else more = 0
}
else
{
std::cout << "Done..." << std::endl;
break;
}
}
}
catch (const zmq::error_t& ze)
{
std::cout << "Exception: " << ze.what() << std::endl;
}
Sleep(5000);
}
Probably C version of code doesn't work either, but you don't check the return code of zmq_recv, so you don't notice it. Also, when receiving miltipart messages you should check if there are more message parts to be received through the socket, like this:
int64_t more = 0;
size_t more_size = sizeof(more);
socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
if (more != 0)
{
//has more parts
}
Also, take a look at ZmqMessage C++ library designed specifically for Sending and receiving ZeroMQ multipart messages.
I decided to use the C version of the code. In general all examples seem to be in C anyway.