免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2322 | 回复: 0
打印 上一主题 下一主题

msgsnd 出错 不是很 明白 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-16 19:34 |只看该作者 |倒序浏览
有这么个client程序:
#include "sys/types.h"
#include "sys/ipc.h"
#include "sys/msg.h"
#include "stdio.h"
#include "stdlib.h"

static struct msgbuf1
{
    long mtype;
    char mtext[100];

} sndbuf, rcvbuf, *msgp ;

extern int errno;

main(int argc, char **argv)

{
    int rtrn, msqid ;
    char name[10];
    double balance;
    if (argc!=2)
        {
            fprintf(stderr,"msgreq [01-99]\n");
            exit(-1);
        }
    if ( (msqid = msgget(0x888, IPC_CREAT|0660)) == -1 )
        {
            fprintf(stderr, "msgget 888 failed !\n");
            exit(-1);
        }
    msgp=&sndbuf;
    sprintf(sndbuf.mtext,"%2.2s",argv[1]);
    printf("输入4位帐号:");
    scanf("%s",&sndbuf.mtext[2]);
    sndbuf.mtext[6]=0;
    msgp->mtype=666;
    rtrn=msgsnd(msqid,msgp, strlen(sndbuf.mtext), 0);
    if (rtrn==-1)
        {
            perror("msgsnd");
            exit(-1);
        }
    msgp=&rcvbuf;
    fprintf(stderr,"等待后台数据处理进程的回答....");
    rtrn=msgrcv(msqid,msgp, 100, atoi(argv[1]), 0);
    if(rtrn==-1)
    {
        perror("msgrcv");
        exit(-1);
    }
    sscanf(rcvbuf.mtext,"%[^|]|%lf",name,&balance);
    printf("\n姓名=%s\n",name);
    printf("余额=%lf\n",balance);
}
这里可以向队列中正常发送消息,server程序也能接收到,server程序如下:
#include "sys/types.h"
#include "sys/ipc.h"
#include "sys/msg.h"
#include "stdio.h"
#include "stdlib.h"

static struct msgbuf1
{
    long mtype;
    char mtext[100];
} sndbuf, rcvbuf , *msgp;

extern int errno;

main()
{
    int rtrn, msgqid ;
    char strbuf[100];
    if ( (msgqid = msgget(0x888, IPC_CREAT|0600)) == -1 )
        {
            fprintf(stderr, "msgget 888 failed !\n"); exit(-1);
        }
    while(1)
    {
        msgp=&rcvbuf;
        fprintf(stderr,"等待前台进程的请求....");
        rtrn=msgrcv(msgqid, msgp, 100, 666 ,MSG_NOERROR);
        if(rtrn==-1)
        {
            perror("msgrcv");
            exit(-1);
        }
    msgp=&sndbuf;
    sprintf(strbuf,"%2.2s\0",rcvbuf.mtext);
    msgp->mtype=atoi(strbuf);
    printf("\n输入帐号=%4.4s的帐户姓名:",&rcvbuf.mtext[2]);
    scanf("%s",sndbuf.mtext);
    strcat(sndbuf.mtext,"|");
    printf("输入该帐户余额:");
    scanf("%s",strbuf);
    strcat(sndbuf.mtext,strbuf);
    rtrn=msgsnd(msgqid, msgp, strlen(sndbuf.mtext), 0);
    if (rtrn==-1)
        {
            perror("msgsnd");
            exit(-1);
        }
    }
}
可是server里面的msgsnd总是发送失败,出现系统提示的:错误消息。
client的打印信息如下:
./client hh
输入4位帐号:1234
等待后台数据处理进程的回答....

server的打印信息如下:
./server
等待前台进程的请求....
输入帐号=1234的帐户姓名:1234
输入该帐户余额:1234
msgsnd: Invalid argument


程序 引用以前帖子 只因我在sco移植到aix也遇到同样问题
希望高手解答

[ 本帖最后由 vagabondyl 于 2007-7-16 19:48 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP