请教一个msg发送程序的问题


 我写了一个msg发送测试代码msg2.c,代码如下.编译连接完成后运行时,提示消息发送失败,错误代码是22,我查了一下,
22表示参数有误。请大家帮我看一下,错误出在哪里?我在xp上装的vmare,vmare上装的ubuntu。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>

#define MAX_TEXT (512)
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};

int main(void)
{
int msg_id=0;
struct my_msg_st send_data;
int send_byte_num=0;

msg_id = msgget((key_t)1234,0666|IPC_CREAT);
if(-1==msg_id)
{
fprintf(stderr,"create msg queue error:%d",errno);
exit(EXIT_FAILURE);
}

memset(&send_data,0,sizeof(send_data));

/*construct msg*/
  send_data.my_msg_type=0;
  send_data.my_some_text[0]='h';
  send_data.my_some_text[1]='e';
  send_data.my_some_text[2]=0;

/*send msg*/
send_byte_num = msgsnd(msg_id,(void*)&send_data,MAX_TEXT ,0);
if(-1 == send_byte_num)
{
fprintf(stderr,"msg sending error:%d",errno); /*这里返回错误代码为22*/
exit(EXIT_FAILURE);
}

  //... other code.

exit(SUCCESS);
}

作者: sunday20060101   发布时间: 2011-03-26

C/C++ code
//fprintf(stderr,"msg sending error:%d",errno);改为
fprintf(stderr,"msg sending failed\n");  

作者: hhbgk   发布时间: 2011-03-26

错误代码贴出来啊。
(void*)&send_data貌似有点错误,需要的只是一个地址就行了,可以改为&send_data试试。后面的大小可以改成sizeof(send_data)-sizeof(long),再试试。

作者: ZFZF294990051   发布时间: 2011-03-26