消息队列发送函数报错,错误码为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;
}