步骤1:同上

步骤2:建立目录

  cd workdir/linux/application/10-comm

  mkdir msg

  cd msg

步骤3:复制

  cp /mnt/hgfs/share/2/Linux系统部分/20.系统消息队列实验/client1.c  ./  -a

  cp /mnt/hgfs/share/2/Linux系统部分/20.系统消息队列实验/client2.c  ./  -a

  cp /mnt/hgfs/share/2/Linux系统部分/20.系统消息队列实验/server.c  ./  -a

步骤4:执行

  gcc client1.c -o client1

  gcc client2.c -o client2

  gcc server.c -o server

  ./server

  打开新终端1

  cd workdir/linux/application/10-comm/msg

   ./client1

  打开新终端2

  cd workdir/linux/application/10-comm/msg

   ./client2

附:程序源码

  client1.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/ipc.h>
 4 #include <sys/msg.h>
 5 #include <string.h>
 6 
 7 #define KEY_MSG  0x101
 8 #define MSGSIZE  64
 9 
10 typedef struct
11 {
12     long  mtype;
13     char  mtext[MSGSIZE];
14 } msgbuf;
15 
16 #define  LEN   sizeof(msgbuf) - sizeof(long)
17 
18 int  main()
19 {
20     int msgid;
21     msgbuf buf1, buf2;
22     msgid = msgget( KEY_MSG, 0666 );
23     while( 1 )
24     {     printf( "input the msg to client2:" );
25         gets( buf1.mtext );
26         buf1.mtype = 1L;
27         msgsnd( msgid, &buf1, LEN, 0 ); //客户端1获取消息并发往服务器
28 
29         msgrcv( msgid, &buf2, LEN, 3L, 0 );  //准备从客户端2获取消息
30         if ( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
31         {  printf( "client1 will quit!\n" ); break;  }
32         printf( "Receive from client2, message: %s\n", buf2.mtext );
33     }
34     return 0;
35 }

  client2.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/ipc.h>
 4 #include <sys/msg.h>
 5 #include <string.h>
 6 
 7 #define KEY_MSG  0x101
 8 #define MSGSIZE  64
 9 
10 typedef struct
11 {
12     long  mtype;
13     char  mtext[MSGSIZE];
14 } msgbuf;
15 
16 #define LEN  sizeof(msgbuf) - sizeof(long)
17 
18 int  main()
19 {
20     int msgid;
21     msgbuf buf1, buf2;
22     msgid = msgget( KEY_MSG, 0666 );
23     while( 1 )
24     { 
25         msgrcv( msgid, &buf2, LEN, 4L, 0 ); //等待客户端1发消息
26         if( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
27         {  printf( "client2 will quit!\n" );
28             break; }
29         else printf( "Receive from client1, message: %s\n", buf2.mtext );
30 
31         sleep(1); //等待一秒,以确保客户端1已经收到了回执
32         printf( "input the msg to client1:" );
33         gets( buf1.mtext );
34         buf1.mtype = 2L;
35         msgsnd( msgid, &buf1, LEN, 0 ); //给客户端1发送回执消息
36     }
37 }

  server.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/types.h>
 4 #include <sys/ipc.h>
 5 #include <sys/msg.h>
 6 #include <string.h>
 7 
 8 #define KEY_MSG  0x101    //使用共有的IPC key
 9 #define MSGSIZE  64
10 
11 typedef struct             //定义消息结构体:消息类型和消息数据
12 {
13     long  mtype;
14     char  mtext[MSGSIZE];
15 } msgbuf;
16 
17 #define  LEN  sizeof(msgbuf)  -  sizeof(long)
18 
19 int  main()
20 {
21     int msgid;
22     msgbuf buf1, buf2;
23     msgid = msgget( KEY_MSG, IPC_CREAT|0666 );
24     while(1) 
25     {
26         msgrcv(msgid, &buf1, LEN, 1L, 0);
27         //接受客户端1的消息
28         printf( "Receive client1 message: %s\n", buf1.mtext );    //打印收到的消息
29         if ( buf1.mtext[0] == 'x' || buf1.mtext[0] == 'X' ) 
30             //若是退出标志,则给2个客户端都发退出信息
31         {  strcpy( buf1.mtext, "x" );
32             buf1.mtype = 3L;
33             msgsnd( msgid, &buf1, LEN, 0 );
34             buf1.mtype = 4L;
35             msgsnd( msgid, &buf1, LEN, 0 );
36             break;
37         }
38         buf1.mtype = 4L;
39         msgsnd( msgid, &buf1, LEN, 0 );   //将客户端1的消息转发给客户端2   
40         msgrcv( msgid, &buf2, LEN, 2L, 0 );  //接受客户端2的消息
41         printf( "Receive client2 message: %s\n", buf2.mtext ); //打印收到的消息
42         if ( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
43             //若是退出标志,则给2个客户端发退出信息
44         {  strcpy( buf2.mtext, "x" );
45             buf2.mtype = 3L;
46             msgsnd( msgid, &buf2, LEN, 0 );
47             buf2.mtype = 4L;
48             msgsnd( msgid, &buf2, LEN, 0 );
49             break;
50         }
51         buf2.mtype = 3L;
52         msgsnd( msgid, &buf2, LEN, 0 );   //将客户端2的消息转发给客户端1
53     }
54     sleep(1);   //若退出,则先等待,以确保客户端程序退出
55     msgctl( msgid, IPC_RMID, NULL );  //删除消息队列,释放空间
56     exit(0);
57 }

 

posted on 2018-08-03 09:52  kingofloong  阅读(603)  评论(0)    收藏  举报