用消息队列编写一个客户端服务器通信的程序
2011-09-20 14:51
776 查看
实验原理:
本实验用消息队列设计一个简易的双人聊天程序(一个服务器,两个客户端)。消息队列重点在于消息类型的匹配,客户端和服务端的“通信协议”的设计。思想如下:
服务器端:接受客户端发来的任何消息,并根据器消息类型,转发给对应的客户端。同时,检测是否有退出标记,有则给所有的客户端发送退出标记,等待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
#define MSGSIZE 64
typedef struct
{
long mtype;
char mtext[MSGSIZE];
}msgbuf;
#define LEN sizeof(msgbuf)-sizeof(long)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,IPC_CREAT|0666);
while(1)
{
msgrcv(msgid,&buf1,LEN,1L,0);
printf("Receive client1 message:%s\n",buf1.mtext);
if(buf1.mtext[0]=='x' || buf1.mtext[0]=='X')
{
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);
msgrcv(msgid,&buf2,LEN,2L,0);
printf("Receive client2 message:%s\n",buf2.mtext);
if(buf2.mtext[0]=='x' || buf2.mtext[0]=='X')
{
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);
}
sleep(1);
msgctl(msgid,IPC_RMID,NULL);
}
客户端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)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,0666);
while(1)
{
printf("Input the mag to client2");
gets(buf1.mtext);
buf1.mtype=1L;
msgsnd(msgid,&buf1,LEN,0);
msgrcv(msgid,&buf2,LEN,3L,0);
if(buf2.mtext[0]=='x'||buf2.mtext[0]=='X')
{
printf("client1 will quit\n");
break;
printf("Recevie from client2,message:%s\n",buf2.mtext);
}
}
}
客户端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)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,0666);
while(1)
{
msgrcv(msgid,&buf2,LEN,4L,0);
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);
printf("Input the msg to clinet1:");
gets(buf1.mtext);
buf1.mtype=2L;
msgsnd(msgid,&buf1,LEN,0);
}
}
本实验用消息队列设计一个简易的双人聊天程序(一个服务器,两个客户端)。消息队列重点在于消息类型的匹配,客户端和服务端的“通信协议”的设计。思想如下:
服务器端:接受客户端发来的任何消息,并根据器消息类型,转发给对应的客户端。同时,检测是否有退出标记,有则给所有的客户端发送退出标记,等待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
#define MSGSIZE 64
typedef struct
{
long mtype;
char mtext[MSGSIZE];
}msgbuf;
#define LEN sizeof(msgbuf)-sizeof(long)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,IPC_CREAT|0666);
while(1)
{
msgrcv(msgid,&buf1,LEN,1L,0);
printf("Receive client1 message:%s\n",buf1.mtext);
if(buf1.mtext[0]=='x' || buf1.mtext[0]=='X')
{
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);
msgrcv(msgid,&buf2,LEN,2L,0);
printf("Receive client2 message:%s\n",buf2.mtext);
if(buf2.mtext[0]=='x' || buf2.mtext[0]=='X')
{
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);
}
sleep(1);
msgctl(msgid,IPC_RMID,NULL);
}
客户端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)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,0666);
while(1)
{
printf("Input the mag to client2");
gets(buf1.mtext);
buf1.mtype=1L;
msgsnd(msgid,&buf1,LEN,0);
msgrcv(msgid,&buf2,LEN,3L,0);
if(buf2.mtext[0]=='x'||buf2.mtext[0]=='X')
{
printf("client1 will quit\n");
break;
printf("Recevie from client2,message:%s\n",buf2.mtext);
}
}
}
客户端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)
void main()
{
int msgid;
msgbuf buf1,buf2;
msgid=msgget(KEY_MSG,0666);
while(1)
{
msgrcv(msgid,&buf2,LEN,4L,0);
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);
printf("Input the msg to clinet1:");
gets(buf1.mtext);
buf1.mtype=2L;
msgsnd(msgid,&buf1,LEN,0);
}
}
相关文章推荐
- Java编写一个简单的TCP通信程序。服务器发送一条字符串,客户端接收该信息并显示。
- 编写和MQTT服务器通信的Android客户端程序(二)
- ACE+线程池实现一个简单的服务器+多客户端通信程序
- Python一个简单的通信程序(客户端 服务器)
- 【实验 1-1】编写一个简单的 TCP 服务器和 TCP 客户端程序。程序均为控制台程序窗口。
- 一个关于消息队列通信的小程序
- 【实验 1-2】编写一个简单的 UDP 服务器和 UDPP 客户端程序。程序均为控制台程序窗口。
- 利用消息队列实现服务器客户端的通信
- C#编写Windows服务程序 (服务端),客户端使用 消息队列 实现淘宝 订单全链路效果
- 一个简单的基于node.js的TCP服务器和基于C++的TCP客户端通信示例程序
- Linux下,使用C/C++编写一个简单的消息处理程序
- 编写Socket客户端和服务器程序,客户端发送一个包含多个数字的字符串给服务器,服务器排序后返回给客户端,要求服务器能连续不断地服务。
- 实现vs2013客户端服务器之间的简单通信,hello world 程序。
- 基于select模型的tcp服务器------一个服务器如何与多个客户端进行通信?
- 使用jrtplib写的一个简单服务器和客户端程序
- 一个客户端服务器通信实现细节
- C++基于TCP/IP简单的客户端、服务器通信程序实例
- C++基于TCP/IP简单的客户端、服务器通信程序实例
- 一个 Java 的 Socket 服务器和客户端通信的例子
- 本程序说明了如何建立一个简单的可以接收UDP服务器数据的UDP客户端