Linux 消息队列实验

【 实验目的】 】
通过此实验,学员可以熟悉消息队列的概念,并能够用消息队列编写一个客户端服务器通信的程序。
【 实验 原理】 】
本实验需要用消息队列设计一个简易的双人聊天程序(一个服务器,两个客户端)。消息队列重点在
于消息类型的匹配,客户端和服务端的“通信协议”的设计。设计思想如下:
服务器端:接受客户端发来的任何信息,并根据其消息类型,转发给对应的客户端。同时,检测是否
有退出标志,有则给所有的客户端发送退出标志,等待 1s 后,确定客户端都退出后,删除消息队列,释
放空间,并退出。
客户端:A 和 B。A 给 B 发送信息,先发给服务器,由服务器根据自定义协议转发该消息给 B。同 B
可以也通过服务器给 A 发消息。
【 实验 方法】 】
服务器端程序:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define KEY_MSG 0x101 //使用共有的 IPC key
#define MSGSIZE 64
typedef struct //定义消息结构体:消息类型和消息数据
{
long mtype;
char mtext[MSGSIZE];
} msgbuf;
#define LEN sizeof(msgbuf) – sizeof(long)
int main()
{
int msgid;
msgbuf buf1, buf2;
msgid = msgget( KEY_MSG, IPC_CREAT|0666 );
while( 1 ) //无限循环,退出标志则会 break
{
msgrcv( msgid, &buf1, LEN, 1L, 0 ); //接受客户端 1 的消息
printf( "Receive client1 message: %s\n", buf1.mtext ); //打印收到的消息
if ( buf1.mtext[0] == 'x' || buf1.mtext[0] == 'X' )
//若是退出标志,则给 2 个客户端都发退出信息
{ strcpy( buf1.mtext, "x" );
buf1.mtype = 3L;
msgsnd( msgid, &buf1, LEN, 0 );
buf1.mtype = 4L;
msgsnd( msgid, &buf1, LEN, 0 );
break;
}
buf1.mtype = 4L;
msgsnd( msgid, &buf1, LEN, 0 ); //将客户端 1 的消息转发给客户端 2
msgrcv( msgid, &buf2, LEN, 2L, 0 ); //接受客户端 2 的消息
printf( "Receive client2 message: %s\n", buf2.mtext ); //打印收到的消息
if ( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
//若是退出标志,则给 2 个客户端发退出信息
{ strcpy( buf2.mtext, "x" );
buf2.mtype = 3L;
msgsnd( msgid, &buf2, LEN, 0 );
buf2.mtype = 4L;
msgsnd( msgid, &buf2, LEN, 0 );
break;
}
buf2.mtype = 3L;
msgsnd( msgid, &buf2, LEN, 0 ); //将客户端 2 的消息转发给客户端 1
}
sleep(1); //若退出,则先等待,以确保客户端程序退出
msgctl( msgid, IPC_RMID, NULL ); //删除消息队列,释放空间
exit(0);
}

客户端 1:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define KEY_MSG 0x101
#define MSGSIZE 64
typedef struct
{
long mtype;
char mtext[MSGSIZE];
} msgbuf;
#define LEN sizeof(msgbuf) – sizeof(long)
int main()
{
int msgid;
msgbuf buf1, buf2;
msgid = msgget( KEY_MSG, 0666 );
while( 1 )
{ printf( "input the msg to client2:" );
gets( buf1.mtext );
buf1.mtype = 1L;
msgsnd( msgid, &buf1, LEN, 0 ); //客户端 1 获取消息并发往服务器
msgrcv( msgid, &buf2, LEN, 3L, 0 ); //准备从客户端 2 获取消息
if ( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
{ printf( "client1 will quit!\n" ); break; }
printf( "Receive from client2, message: %s\n", buf2.mtext );
}
return 0;
}

客户端 2:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define KEY_MSG 0x101
#define MSGSIZE 64
typedef struct
{
long mtype;
char mtext[MSGSIZE];
} msgbuf;
#define LEN sizeof(msgbuf) – sizeof(long)
int main()
{
int msgid;
msgbuf buf1, buf2;
msgid = msgget( KEY_MSG, 0666 );
while( 1 )
{
msgrcv( msgid, &buf2, LEN, 4L, 0 ); //等待客户端 1 发消息
if( buf2.mtext[0] == 'x' || buf2.mtext[0] == 'X' )
{ printf( "client2 will quit!\n" );
break; }
else printf( "Receive from client1, message: %s\n", buf2.mtext );
sleep(1); //等待一秒,以确保客户端 1 已经收到了回执
printf( "input the msg to client1:" );
gets( buf1.mtext );
buf1.mtype = 2L;
msgsnd( msgid, &buf1, LEN, 0 ); //给客户端 1 发送回执消息
}
}

运行测试。先编译运行服务器,确保消息队列已经创建完毕,并进入等待状态。结果如下:
[root@localhost root]# ./server
Receive client1 message: i 'am client A
Receive client2 message: ok! I have recieved your msg,I'm client B
Receive client1 message: x
[root@localhost root]#


客户端 A 的运行结果如下:
[root@localhost root]# ./client1
input the msg to client2:i 'am client A
Receive from client2, message: ok! I have recieved your msg,I'm client B
input the msg to client2:x
client1 will quit!
[root@localhost root]#


客户端 B 的运行结果如下:
[root@localhost root]# ./client2
Receive from client1, message: i 'am client A
input the msg to client1: ok! I have recieved your msg,I'm client B
client2 will quit!
[root@localhost root]#

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

STM32单片机定制

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值