cwzx/nngpp

Send and receive a structure?

colesnicov opened this issue · 1 comments

Hello. Can I send and receive a structure?
Something like this:

Structure in both apps:

struct Data_t{
  int code;
  char* msg;
};

First process:

Data_t data{5, "ok"};
socket.send(data);
...

Second process:

Data_t data;
data = socket.recv();
int code = data.code;
...

I've found the answer here

Structure:

struct Data_t{
  int code;
  char msg[10]; // no pointer to chars array, fixed size array!?
};

first process:

Data_t data{ 0, '\0' };
data.code = 5;
memcpy(data.msg, "test", 4);

nng::buffer v = nng::buffer(sizeof(Data_t));
memcpy(v.data(), &t, sizeof(Data_t));

socket.send(v);

Second process:

auto buf = socket.recv();
Data_t data{0, '\0'};
memcpy(&data, buf.data(), sizeof(Data_t));

std::cout << data.msg; // test