把 struct msg 改一下,

struct msg
{
    long int msgtype;
    int   count;
    unsigned int data[35];
}

其中count记录总共有多少个 data,这样就可以发送变长的消息了,比如你要发10个unsigned int,那么
struct msg mymsg;
mymsg.msgtype = XXXX;
mymsg.count = 10;

/* 发消息时,不用把整个msg都发出去,只需要发有效的内容 */
len = sizeof(msg) - sizeof(unsigned int) * (35-mymsg.count);
mq_send(queue, &mymsg, len, 0);

/* 接收后, 根据收到的 count来取数据 */
struct msg rcvmsg;
mq_receive(queue, &rcvmsg, sizeof(rcvmsg), NULL);
printf("%d data received\n", rcvmsg.count);

  

 

 

http://www.sdembed.com/thread-137-1-1.html

相关文章:

  • 2021-11-19
  • 2022-01-22
  • 2021-11-19
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
猜你喜欢
  • 2021-12-14
  • 2021-12-22
  • 2022-12-23
  • 2021-04-28
  • 2021-09-23
  • 2021-12-10
相关资源
相似解决方案