Send structure to mq_send

I use POSIX IPC and according to the documentation - http://man7.org/linux/man-pages/man3/mq_send.3.html

The mq_send () method sends only char * data, and mq_recv () receives only character data. However, I want to send the user structure to my msg queue, and on the receiving side I want to get the structure.

sample struct:

struc Req { pid_t pid; char data[4096]; } 

So, does anyone know how to do this in C lang?

+6
source share
2 answers

You just need to pass the address of the structure and apply it to the corresponding pointer type: const char * for mq_send and char * for mq_receive .

 typedef struct Req { pid_t pid; char data[4096]; } Req; Req buf; n = mq_receive(mqdes0, (char *) &buf, sizeof(buf), NULL); mq_send(mqdes1, (const char *) &buf, sizeof(buf), 0); 
+8
source

You can use memcpy as follows:

 char * data; //Do appropriate allocation. memcpy(data, &req, sizeof(req))); 

Upon receipt, you copy the received data to the structure.

 memcpy(&rec, data, sizeof(rec))); 
-1
source

All Articles