0

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.

user3666197
  • 1
  • 6
  • 50
  • 92
Pablo
  • 28,133
  • 34
  • 125
  • 215
  • What is in the first 15 bytes? If it is a header, perhaps it has the offset to data encoded there. If not, you'll have no choice but to hard code the offset. – Kevin May 23 '16 at 18:44
  • Looking in C++ code, first 8 byte is `int64_t file_desc` and other 8 byte is `metadata_t *metadata` inside struct. Then comes data. I was hoping that someone with knowledge of clrzmq4 library may give hint how to handle it. Or perhaps recommend other .NET framework which has data extraction routines. – Pablo May 23 '16 at 18:47
  • Be carefull on how multipart messages are being assembled in ZeroMQ. As explained in your other post, if not developing intentionally on a low-level, avoid to use the zmq_msg_t directly and use the higher-level API interface. – user3666197 May 23 '16 at 21:09
  • Also may have noticed Steve Balousek's reminder about **changed 32B -> 64B `zmq_msg_t` sizing, since version 4.1.x**, so non-homogeneous deliveries shall operate on high-level services, not byte-scale/alignment **details >>> http://stackoverflow.com/a/37107166/3666197** – user3666197 May 24 '16 at 22:36

1 Answers1

2

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);
David
  • 1,510
  • 14
  • 20