You cannot send string over network as std::string
is a container. You can use flexible array member or big size array or write a small class which is serializable (you have to write code by yourself to prepare buffer) to send the data.
When you do struct structB messageB = messageC->b;
, pointer members of std::string
member embed inside messageC->b might be dereference in copy constructor or std::string
which is possibly causing the segmentation fault.
Example of large char array would be:
struct structB{
int a;
char c[MAX_LENGTH];
};
Later
struct structB messageB;
messageB.a=0;
strcpy(messageB.c,"aa"); // #include<cstring> or use std::copy from <algorithm>
struct structC *messageC = new struct structC;
// I guess you want this(not sure) messageC->z = static_cast<int>( sizeof(int) + strlen(messageB) + 1 );
messageC->z = 4;
messageC->b = messageB;
And then
const int length = sizeof(int) /* z */ + sizeof(int) /* a */ + strlen("aa") + 1;
zmq::message_t msg (length);
memcpy (msg.data (), &messageC, length);
socket->send(msg);
These are some changes required at server side, you need to make similar changes at client side also.
As a side note, your code is very messy, don't deploy it in larger applications before sorting out few things like removal of unnecessary new
and properly representing nested structures.