How to send integer with message queue with POSIX API in linux?

不打扰是莪最后的温柔 提交于 2019-12-22 18:12:59

问题


I try to send integer by msg queue but the function mq_send(mq, &val , sizeof(val), 0); is working for only char type pointer so is there any way to send integer to queue with another function or same function.

Regards...


回答1:


Do not read the char* in this case as the only allowed datatype.

Many *ix API use char as a generic buffer pointer.

View the interface therefore as taking a pointer to buffer and the size of the buffer.

That buffer can be anything you like, from a single int, to a struct, seralized string representation of your class, or just about anything else in memory.

int i;
mq_send(mq, (char *) &i, sizeof(i), 0);

Should work (not tested)

Good Luck



来源:https://stackoverflow.com/questions/5247041/how-to-send-integer-with-message-queue-with-posix-api-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!