[zeromq-dev] dealing with binary bytes
Andrew Ukrainec
aukrainec at accipiterradar.com
Thu Jun 7 19:29:15 CEST 2012
Hi,
Does it makes sense to add the following extensions
to czmq?
My interest is in sending frames of formatted, variable
length binary byte data rather than character strings.
This means that you can't rely on the null terminator
as a reliable terminator for the end of the data, since
its a valid byte value in the data. Instead, I need
to explicitly specify the length of the message, and
similarly return the length of the message on receive.
// modified from zstr_send() to add explicit length
int zstr_send_with_length(void *socket, const char *string, int N)
{
assert (socket);
assert (string);
zmq_msg_t message;
zmq_msg_init_size(&message, N);
memcpy(zmq_msg_data (&message), string, N);
int rc = zmq_sendmsg(socket, &message, 0);
zmq_msg_close(&message);
return rc < 0? -1: 0;
}
// modified from zstr_recv() to return length
char *zstr_recv_with_length(void *socket, int *N)
{
assert (socket);
zmq_msg_t message;
zmq_msg_init (&message);
if (zmq_recvmsg (socket, &message, 0) < 0) return NULL;
int size = zmq_msg_size (&message);
*N = (int)size;
char *string = (char *)malloc (size + 1);
memcpy (string, zmq_msg_data(&message), size);
zmq_msg_close (&message);
string[size] = 0;
return string;
}
--
Dr. Andrew Ukrainec
Principal Engineer e-mail: aukrainec at accipiterradar.com
Accipiter Radar Technologies Inc. http://www.accipiterradar.com
Niagara, Ontario Tel: (905) 228-6888 Fax: (905)892-2249
Orchard Park, New York Tel: (716) 508-4432 Fax: (888)393-6421
NOTICE: This e-mail transmission and its attachments are intended solely for
the recipients and may contain information that is proprietary or provided as
commercial-in-confidence. Should you receive this e-mail or its attachments
and are not an intended recipient, you are hereby requested to notify the
sender and to delete and not forward the received information to any other
party.
More information about the zeromq-dev
mailing list