How to extract data from message in C# ZeroMQ?

烂漫一生 提交于 2020-01-07 06:34:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!