I am new to zeromq and I want to use zeromq to send a self-defined struct. The struct is filled inside a C language project, and I use this struct to collect some log info from the C project. Then I send this struct to the log store.
struct msg {
char * item1;
unsigned int amount1;
char * name1;
unsigned int cost1;
...
};
There are about 10 strings with variable length, and I don't know how to send this struct to zeromq. One method is separate this struct to 10 parts, each part i can get the string and length, then use zmq_msg_send 10 times. But I wonder whether there is a more elegant way to do this.
Thanks!
I tried to search Google and the official document. No examples solve my question.
You need to serialize your structure, which is to say create a single sequence of bytes from it.
You could roll out your own format. For example, you could allocate an array large enough to hold all the strings (including the terminal NUL) and other values and concatenate them all using memcpy
. But there are pitfalls and it could be a fair amount of work.
Alternatively, you could serialize it into JSON or YAML, but they're primarily intended to produce something that's still human readable. Other formats exist with binary messages in mind, but you might not have heard of them (Protocol Buffers, FlatBuffers, etc).
Yet another approach would be to store the data in a commonly accessible location (such as a database) and merely send the id of the record.