问题
I need to send data from C++
to C#
.
On C++
side, under Linux, I am using ZMQ
library version 4.1.4
.
C#
side is using clrzmq4 library based on 4.1.5 version
.
So the part where I send message in C++
:
char tempStr[] = "ABCD";
zmq_msg_t zmsg;
zmq_msg_init_size(&zmsg, 4);
memcpy(zmq_msg_data(&zmsg), tempStr, 4);
int rc = zmq_send(reqSocket, &zmsg, sizeof(zmsg), 0);
zmq_msg_close(&zmsg);
C#
code to retrieve message:
ZFrame request = responder.ReceiveFrame();
byte[] reqBytes = new byte[100];
int n = request.Read(reqBytes, 0, 100);
The problem is that byte array is including all 64 bytes of zmq_msg_t
. The actual data is starting from offset 16.
Question
- how to properly extract data in this case? Extracting by hard coding the offset in my code is simply ugly, because one day zmq_msg_t
may be changed from sender side and data will be located somewhere else. Other option is to avoid using zmq_msg_t
, when both sides are not using same platform/framework. In the clrzmq4
framework I can see there are delegates for zmq_msg_t
types, but not sure how to use them and whether they intended for public usage.
回答1:
Your mixing the send types on the c++ side, if using zmq_msg_t you dont need the variant with a size, thats for sending a buffer.
If using the buffer function
int zmq_send (void *socket, void *buf, size_t len, int flags)
Then you should do
zmq_send(reqSocket, tempStr, 4, 0);
However if using zmq_msg_t
variant
int zmq_msg_send (zmq_msg_t *msg, void *socket, int flags)
Then you should do :
zmq_msg_send (&zmsg, reqSocket, 0);
来源:https://stackoverflow.com/questions/37397992/how-to-extract-data-from-message-in-c-sharp-zeromq