如何在Linux中使用POSIX API发送带有消息队列的整数? [英] How to send integer with message queue with POSIX API in linux?

查看:75
本文介绍了如何在Linux中使用POSIX API发送带有消息队列的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过msg队列发送整数,但是函数mq_send(mq,& val,sizeof(val),0);仅适用于char类型的指针,因此有任何方法可以将整数发送到另一个函数或相同函数的队列中.

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.

关于...

推荐答案

在这种情况下,请勿读取 char * 作为唯一允许的数据类型.

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

许多* ix API使用char作为通用缓冲区指针.

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.

该缓冲区可以是您喜欢的任何东西,从单个int到类的结构化,序列化的字符串表示形式,或者内存中几乎所有其他内容.

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);

应该工作(未经测试)

祝你好运

这篇关于如何在Linux中使用POSIX API发送带有消息队列的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆