[zeromq-dev] proper way to handle messages with c structures
Andrew Simpson
simpsonar77 at gmail.com
Thu Jul 9 22:17:04 CEST 2015
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 client
2. 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150709/42179247/attachment.htm>
More information about the zeromq-dev
mailing list