Chinaunix首页 | 论坛 | 博客
  • 博客访问: 784511
  • 博文数量: 161
  • 博客积分: 10005
  • 博客等级: 中将
  • 技术积分: 1445
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-04 15:08
文章分类

全部博文(161)

文章存档

2014年(1)

2013年(1)

2011年(2)

2010年(18)

2009年(26)

2008年(18)

2007年(66)

2006年(29)

我的朋友

分类: C/C++

2007-01-18 14:20:58

dlserver.c是服务端,服务端读取队列的信息
dlclient.c是客户端,客户端发送队列信息
求解:
为什么我的服务端读取的数据是process1 m而不是process1 message?
 
dlserver.c
 
#include
#include
#include
#include
#include
//msgid
static int msg_id=-1;
//send and recv struct
struct msgbuf
{
 long int m_type;
 char m_text[1024];
};
/*
*int msgid msgid;char *msgp send message
*long int type message type
*return 0:true;-1:false
*/
int msgsend(int msgid,char *msgp,long int type)
{
 struct msgbuf send_msg;
 int ret = 0 ;
 send_msg.m_type=type;
 strcpy(send_msg.m_text,msgp);
 //printf send message info
 //printf("send msg=%s\n",send_msg.m_text);
 //send msg
 ret=msgsnd(msgid,&send_msg,10,0);
 if(ret<0)
 {
  perror("send message error!");
  return -1;
 }
 return 0;
}
/*
*int msgid msgid;char *msgp send message
*long int type message type
*return 0:true;-1:false
*/
int msgrecv(int msgid,char *msgp,long int type)
{
 struct msgbuf recv_msg;
 int ret = 0;
 //recv msg
 ret=msgrcv(msgid,&recv_msg,sizeof(struct msgbuf)-sizeof(long),type,0);
 if(ret<0)
 {
  perror("recv message error!");
  return -1;
 }
 //printf recv message info
 //printf("msgrecv recv message: %s \n",recv_msg.m_text);
 strcpy(msgp,recv_msg.m_text);
 return 0;
}
int main(int argc,char **argv)
{
 struct msgbuf recv_msg,send_msg;
 int ret=0;
 int pid;
 char recvmsg[1024];
 memset(recvmsg,0,sizeof(recvmsg));
 //create msg
 msg_id=msgget(1,IPC_CREAT|0666);
 if(msg_id==-1)
 {
  perror("create new message list error!");
  exit(1);
 } 
 
 //process recv message
 pid=fork();
 switch(pid)
 {
 case -1:
  perror("fork error!");
  exit(1);
 case 0:
  while(1)
  {
   if((msgrecv(msg_id,recvmsg,1))==-1)
   {
    perror("process recv message error!");
    return -1;
   }
   printf("process recv message= %s \n",recvmsg);
  }
 default:
  wait(NULL);
 }
 return 0; 
}
 
dlclient.c
 
#include
#include
#include
#include
#include
//msgid
static int msg_id=-1;
//send and recv struct
struct msgbuf
{
 long int m_type;
 char m_text[1024];
};
/*
*int msgid msgid;char *msgp send message
*long int type message type
*return 0:true;-1:false
*/
int msgsend(int msgid,char *msgp,long int type)
{
 struct msgbuf send_msg;
 int ret = 0 ;
 send_msg.m_type=type;
 strcpy(send_msg.m_text,msgp);
 //printf send message info
 //printf("send msg=%s\n",send_msg.m_text);
 //send msg
 ret=msgsnd(msgid,&send_msg,10,0);
 if(ret<0)
 {
  perror("send message error!");
  return -1;
 }
 return 0;
}
/*
*int msgid msgid;char *msgp send message
*long int type message type
*return 0:true;-1:false
*/
int msgrecv(int msgid,char *msgp,long int type)
{
 struct msgbuf recv_msg;
 int ret = 0;
 //recv msg
 ret=msgrcv(msgid,&recv_msg,sizeof(struct msgbuf)-sizeof(long),type,0);
 if(ret<0)
 {
  perror("recv message error!");
  return -1;
 }
 //printf recv message info
 //printf("msgrecv recv message: %s \n",recv_msg.m_text);
 strcpy(msgp,recv_msg.m_text);
 return 0;
}
int main(int argc,char **argv)
{
 struct msgbuf recv_msg,send_msg;
 int ret=0;
 int pid;
 int type;
 char sendmsg[1024];
 memset(sendmsg,0,sizeof(sendmsg));
 //create msg
 msg_id=msgget(1,IPC_CREAT|0666);
 if(msg_id==-1)
 {
  perror("create new message list error!");
  exit(1);
 } 
 
 sprintf(sendmsg,"%s%s%s","process",argv[1]," message");
 
 type=atoi(argv[2]);
 //first process send message
 pid=fork();
 switch(pid)
 {
 case -1:
  perror("fork 1 error!");
  exit(1);
 case 0:
  if((msgsend(msg_id,sendmsg,type))==-1)
  {
   perror("first process send message error!");
   return -1;
  }
 default:
  wait(NULL);
 }
 return 0; 
}
阅读(1300) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~