[zeromq-dev] proper way to handle messages with c structures
Andrew Simpson
simpsonar77 at yahoo.com
Fri Jul 10 04:27:36 CEST 2015
Understood completely. I actually already do serialization in the application. (I'm even in the process of converting over to google protobufs as you mentioned).
Also, my limited examples were a little lazy. I declare ints as uint32, etc...
My bigger question is what is the proper way to invoke zmq when using structs? Using higher level calls like zmq_send or zmq_recv? or by working with the lowest level zmq functions/frames/etc...?
thanks!
On Thursday, July 9, 2015 5:52 PM, Jens Auer <jens.auer at betaversion.net> wrote:
#yiv7715293402 #yiv7715293402 -- _filtered #yiv7715293402 {font-family:Wingdings;panose-1:5 0 0 0 0 0 0 0 0 0;} _filtered #yiv7715293402 {font-family:Wingdings;panose-1:5 0 0 0 0 0 0 0 0 0;} _filtered #yiv7715293402 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv7715293402 {font-family:Tahoma;panose-1:2 11 6 4 3 5 4 4 2 4;}#yiv7715293402 #yiv7715293402 p.yiv7715293402MsoNormal, #yiv7715293402 li.yiv7715293402MsoNormal, #yiv7715293402 div.yiv7715293402MsoNormal {margin:0cm;margin-bottom:.0001pt;font-size:12.0pt;}#yiv7715293402 a:link, #yiv7715293402 span.yiv7715293402MsoHyperlink {color:blue;text-decoration:underline;}#yiv7715293402 a:visited, #yiv7715293402 span.yiv7715293402MsoHyperlinkFollowed {color:purple;text-decoration:underline;}#yiv7715293402 p.yiv7715293402MsoListParagraph, #yiv7715293402 li.yiv7715293402MsoListParagraph, #yiv7715293402 div.yiv7715293402MsoListParagraph {margin-top:0cm;margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;font-size:12.0pt;}#yiv7715293402 span.yiv7715293402E-MailFormatvorlage17 {color:#1F497D;}#yiv7715293402 .yiv7715293402MsoChpDefault {} _filtered #yiv7715293402 {margin:70.85pt 70.85pt 2.0cm 70.85pt;}#yiv7715293402 div.yiv7715293402WordSection1 {}#yiv7715293402 _filtered #yiv7715293402 {} _filtered #yiv7715293402 {} _filtered #yiv7715293402 {} _filtered #yiv7715293402 {font-family:Wingdings;} _filtered #yiv7715293402 {font-family:Symbol;} _filtered #yiv7715293402 {} _filtered #yiv7715293402 {font-family:Wingdings;} _filtered #yiv7715293402 {font-family:Symbol;} _filtered #yiv7715293402 {} _filtered #yiv7715293402 {font-family:Wingdings;}#yiv7715293402 ol {margin-bottom:0cm;}#yiv7715293402 ul {margin-bottom:0cm;}#yiv7715293402 Hi, it is not recommended to use memcopied structs in C (or even worse classes in C++) as an exchanged format. There are many reasons why this can go wrong:- You do not consider different byte-order- The size of int,short etc. is not defined in C/C++. If you run the server on a 64-bit system, and the client on 32-bit system, you will run into problems- The compiler is free to add padding between the struct members, e.g. to adhere alignment constraints These are just the first things that come to my mind. The result is that an object of InitMsg can look very different in memory on two plattforms, even if both are running the same Linux version. Even on the same platform with identical operating systems, the layout could be different when client and server use different compiler options. I would advise to use a serialization/deserialization framework to convert the structs into and from a well-defined representation. Google protocol buffers are often recommended, and I have seen C code using xdr (although I would never ever recommend this). Best wishes, Jens Von: zeromq-dev-bounces at lists.zeromq.org [mailto:zeromq-dev-bounces at lists.zeromq.org] Im Auftrag von Andrew Simpson
Gesendet: Donnerstag, 9. Juli 2015 22:17
An: zeromq-dev at lists.zeromq.org
Betreff: [zeromq-dev] proper way to handle messages with c structures I am modifying an existing application to use ZMQ to send/receive the data. The application is written in C on RHEL 6.6, using ZMQ 4.0.5. I am using router-dealer socket types. I do have the messages coming through right now, but I do not think I am doing this in the best possible way: The application consists of a "server" and multiple "client" applications. These are separate binaries, not threads. I am setting the ID on the Dealer socket for each client application. A message containing a C structure is passed from the client to the server. An example C struct that I use: struct InitMsg { char myName[10]; int myUUID; int someVar; short someOtherVar;}; The Client is the Dealer, server is the router socket. METHOD 1:Client sends the message: zmq_send(serverSocket, &InitMsg, sizeof(InitMsg), 0); On the server side, I do the following:1. zmq_recv once to get the ID of the client2. call a second zmq_recv to get the message from the client. while the above seems to be working, It seems that I should be using something more like:METHOD 2:client:zmq_msg_t newMsg;zmq_msg_init (&newMsg);zmq_msg_copy (&newMsg, &InitMsg);zmq_msg_send (&newMsg, serverSocket, 0); server: zmq_msg_t *msg;zmq_msg_init(&msg);zmq_msg_recv(&msg, clientSocket, 0); I'm looking for a recommendation about the best/most efficient way to handle sending/receiving c structures and I've gotten a little confused about everything :( There appears to be many ways to do this. I have no problem working directly with zmq objects/frames, etc... but if zmq_recv.zmq_send is just as efficient, I'm fine with it. A few additional questions: 1. After reading the guide and examples, does the second method above handle getting the id, delimiter, and the message all in one? 2. Am I correct in that a router/dealer pattern, that the send is always: ID, delimiter, message? Thanks for all your help. Drew
_______________________________________________
zeromq-dev mailing list
zeromq-dev at lists.zeromq.org
http://lists.zeromq.org/mailman/listinfo/zeromq-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150710/e01a08dd/attachment.htm>
More information about the zeromq-dev
mailing list