消息队列发送函数报错,错误码为22。谢谢回答

laotousheng 2012-09-03 10:20:22
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <sys/ipc.h>


struct stClient
{
int iConCounter; /*连接次数*/
int iConId; /*客户端创建ID号*/
int iStatistics; /*服务端接收的次数,做为映射关系标识*/
};

/*消息队列结构体*/
struct stMsgs
{
long mtype;
struct stClient stMsg;
};

/*创建消息队列*/
int msgcreat (int iKey)
{
int iMsgid = 0;

printf("消息队列中获取的KEY值为%d\n", iKey);

/*创建消息队列*/
iMsgid = msgget (iKey, 0666 | IPC_CREAT);/*创建消息队列*/
if (iMsgid <0)
{
printf("创建消息对列错误,错误码为%d", errno);
return -1;
}

printf("创建消息队列成功,消息队列描述符为%d", iMsgid);

return 0;

}


/*消息对列关键字获取函数*/
int msggain()
{
int iMsgid;
int iKey;

iKey = 4321; /*获取Key值*/
iMsgid = msgget (iKey, 0666 | IPC_CREAT); /*打开消息队列*/

if (iMsgid < 0)
{
printf("打开消息队列错误,错误码为%d", errno);
return -1;
}

printf("打开消息队列成功,消息队列描述符为%d", iMsgid);

return iMsgid;
}

msgsnd: Invalid or incomplete multibyte or wide character
错误码为22


/*发送消息队列函数*/
int msgsend (int iMsgid, struct stMsgs *stMsgSend)
{
int iRet;

/*发送消息*/
while(1) /*只发送一次,成功就跳出循环*/
{
iRet = msgsnd (iMsgid, stMsgSend, sizeof((*stMsgSend).stMsg), 0);
if(iRet < 0)
{
if(errno == EINTR)
{
printf("信号中段发送错误");
continue;
}
else
{
printf("消息队列ID号为%d,发送数据为%d %d %d", iMsgid, (*stMsgSend).stMsg.iConCounter,
(*stMsgSend).stMsg.iConId, (*stMsgSend).stMsg.iStatistics);

printf("消息队列发送系统错误,错误码为%d", errno);
perror("msgsnd");

return -1;
}
}

break;/*发送成功跳出循环*/
}
printf("消息队列发送成功");
return 0;
}


/*接收消息队列函数*/
int msgreceive (int iMsgid)
{
int iRet;
struct stClient stReceive;

while (1) /*将消息队列中所有消息输出*/
{

iRet = msgrcv(iMsgid, &stReceive, sizeof(stReceive), 0, 0);
if(iRet < 0)
{
if (errno == EINTR)
{
printf("信号中段接收错误");
continue;
}
else
{
printf("消息队列系统错误接收失败,错误码为%d", errno);
return -1;
}
}

/*将消息队列接收数据输出*/
printf("与共享内存的映射关系为%d\n", stReceive.iStatistics);
printf("客户端连接次数为%d\n", stReceive.iConCounter);
printf("客户端的socket描述字为%d\n", stReceive.iConId);
}

return 0;
}



int main()
{
int iKey;
int iMsgId;
struct stMsgs stMsgSend;

memset(&stMsgSend, 0x0, sizeof(stMsgSend));

stMsgSend.mtype = msggain();
stMsgSend.stMsg.iConCounter = 2;
stMsgSend.stMsg.iConId = 1;
stMsgSend.stMsg.iStatistics = 10;

msgcreat(4321);
iMsgId = msggain();
msgsend (iMsgId, &stMsgSend);
msgreceive(iMsgId);

return 0;
}
...全文
766 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndyZhang 2012-09-04
  • 打赏
  • 举报
回复
msgsnd: Invalid or incomplete multibyte or wide character
非法的或者不完整的多字节或者宽字节,应该是你发送的消息有问题吧。看看你的编译环境设置的是多字节还是unicode。。。
guochanoo7 2012-09-04
  • 打赏
  • 举报
回复
你这代码也不了解函数的机理,不好整,首先你是while循环来测试这个发送函数是不是成功了,然后如果msgrcv在执行一半的时候来了一个int信号,那处理完以后msgrcv是可重入的吗,能继续执行吗。其实你可以使用阻塞的方式,比如select,这个也就是猜测,希望有点帮助
luguangxu68 2012-09-04
  • 打赏
  • 举报
回复
/*接收消息队列函数*/
int msgreceive (int iMsgid)
{
int iRet;
struct stClient stReceive;

while (1) /*将消息队列中所有消息输出*/
{

iRet = msgrcv(iMsgid, &stReceive, sizeof(stReceive), 0, 0);

里面有错误啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


/*接收消息队列函数*/
int msgreceive (int iMsgid)
{
int iRet;
struct stMsgs stReceive;

while (1) /*将消息队列中所有消息输出*/
{

iRet = msgrcv(iMsgid, &stReceive, sizeof(stReceive.stMsg), 0, 0);

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧