Question

I have client and server code communicating with each other using protobuf-c over 0mq

below is the client cod snipped

void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_REQ);
char collector[100]; 

char buffer[MAX_MSG];

sprintf(collector,"tcp://%s:%s",argv[2],argv[3]);
zmq_connect (requester, collector);

LogMsg proto_msg = LOG_MSG__INIT;
void *buf;      // Buffer to store serialized data
unsigned len;   // Length of serialized data  
proto_msg.name=argv[1];//assigning arg1 the node name
proto_msg.ip="127.0.0.1"; //TODO get IP im connected to
proto_msg.pid=getpid();
proto_msg.has_pid = 1;

//sending Hello to Collector to register 
proto_msg.level = "HELLO";
len = log_msg__get_packed_size (&proto_msg);
buf = malloc (len);         
log_msg__pack (&proto_msg, buf);  
printf("Writing %d serialized bytes to socket: Handshaking with Collector\n",len); // See the length of message
zmq_send(requester, buf,len, 0);

on the Server side i have the following code

#include "generator.h"

int main (void)
{
    //  Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
uint8_t buffer[MAX_MSG];
int i=0;
assert (rc == 0);
zmq_recv (responder, buffer, MAX_MSG, 0);
printf ("recieved -> %s\n",buffer);
LogMsg *ptr_msg; 
ptr_msg = log_msg__unpack(NULL,i,buffer); // Deserialize the serialized input
if (ptr_msg == NULL)
{ // Something failed
    fprintf(stderr,"error unpacking incoming message\n");
    return 1;
}

log_msg__free_unpacked(ptr_msg,NULL);
    return 0;
}

But when the server receives the message it is giving the following error

recieved -> 
B1  127.0.0.1??"HELLO/?? 
message 'Log_msg': missing required field 'name'

For reference .proto file is as below

message Log_msg
{
required string name=1; //Node Name
optional string IP=2 [default = "127.0.0.1"]; //Node IP Addr
optional int32 PID=3 [default = 9999]; //Process ID
optional string level=4 [default = "INFO"]; //Log Info Level
optional int64 datim=5; //UNIX Time Stamp
optional string msg=6; //Log message

}

I assume when i am transmitting it on wire then it gets corrupted. Any ideas how i can send it properly. Examples online show this for c++. I need for C.

Was it helpful?

Solution

zmq_send and zmq_recv seems to handle zmq_msg_t structures rather than just the buffer.

Maybe you should try to create such structures rather than just sending your buffer ?

See the examples in http://api.zeromq.org/2-1:zmq-send and http://api.zeromq.org/2-1:zmq-recv

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top