Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...