![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
2020.07.18
linux分享
●
●
●
#01#前言
前言:
在整理上学期资料同时将部分代码分享,将以代码书写时间为时间线,按优化程度逐步发出,代码均为在linux下c编程。
本文分成3部分:
1、图片演示
2、代码展示及说明
3、下载地址
#OVER#
![f0a4a4562bfcd5dbc736297f77b3e904.png](https://img-blog.csdnimg.cn/img_convert/f0a4a4562bfcd5dbc736297f77b3e904.png)
#02#图片演示
![ad91c1cb4fa83d359a05d8ea962b2de8.png](https://img-blog.csdnimg.cn/img_convert/ad91c1cb4fa83d359a05d8ea962b2de8.png)
#OVER#
![f0a4a4562bfcd5dbc736297f77b3e904.png](https://img-blog.csdnimg.cn/img_convert/f0a4a4562bfcd5dbc736297f77b3e904.png)
#03#具体代码
说明在下方
fwq.c
/*
* fwq.c
*
* Created on: 2020年5月17日
* Author: wu
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include "xxlx.h"
int main(int argc, char *argv) {
key_t key=ftok("/home/wu/key",'c');
int msgid = msgget(key,IPC_CREAT|0777);
while(1){
struct message _msg;
memset(&_msg,0,sizeof(struct message));
if(msgrcv(msgid,&_msg,64,c1_fwq,IPC_NOWAIT)>0){
_msg.type=fwq_c2;
msgsnd(msgid,&_msg,64,IPC_NOWAIT);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}
memset(&_msg,0,sizeof(struct message));
if(msgrcv(msgid,&_msg,64,c2_fwq,IPC_NOWAIT)>0){
_msg.type=fwq_c1;
msgsnd(msgid,&_msg,64,IPC_NOWAIT);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}
}
sleep (1);
msgctl(msgid,IPC_RMID,NULL);
return 0;
}
khd1.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "xxlx.h"
#define MYMSG_TYPE 10
int main(int argc, char **argv) {
key_t key=ftok("/home/wu/key",'c');
int msgid = msgget(key,IPC_CREAT|0777);
char buf[64];
pid_t pid=fork();
if(pid>0){
while(1){
memset(buf,0,64);
fgets(buf,64,stdin);//获取输入
struct message _msg;
memset(&_msg,0,sizeof(struct message));
_msg.type=c1_fwq;
strncpy(_msg.data,buf,strlen(buf));
msgsnd(msgid,&_msg,64,0);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}
}
else if(pid==0){
while(1)
{
struct message _msg;
memset(&_msg,0,sizeof(struct message));
msgrcv(msgid,&_msg,64,fwq_c1,0);
printf("recv:%s\n",_msg.data);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}}
return 0;
}
khd2.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "xxlx.h"
#define MYMSG_TYPE 10
int main(int argc, char **argv) {
key_t key=ftok("/home/wu/key",'c');
int msgid = msgget(key,IPC_CREAT|0777);
char buf[64];
pid_t pid=fork();
if(pid>0){
while(1){
struct message _msg;
memset(&_msg,0,sizeof(struct message));
msgrcv(msgid,&_msg,64,fwq_c2,0);
printf("recv:%s\n",_msg.data);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}
}
else if(pid==0){
struct message _msg;
while(1){
memset(buf,0,64);
fgets(buf,64,stdin);
memset(&_msg,0,sizeof(struct message));
_msg.type=c2_fwq;
strncpy(_msg.data,buf,strlen(buf));
msgsnd(msgid,&_msg,64,0);
if(strncmp(_msg.data,"quit",4)==0){
break;
}
}}
return 0;
}
头文件:xxlx.h/*
* xxlx.h
*
* Created on: 2020年5月17日
* Author: wu
*/
#ifndef XXLX_H_
#define XXLX_H_
#define c1_fwq0x01
#define c2_fwq0x02
#define fwq_c20x03
#define fwq_c10x04
struct message
{
long type;
char data[64];
};
#endif /* XXLX_H_ */
#OVER#
![f0a4a4562bfcd5dbc736297f77b3e904.png](https://img-blog.csdnimg.cn/img_convert/f0a4a4562bfcd5dbc736297f77b3e904.png)
#04#说明
代码说明
1、服务器使用while循环,不断对二个客户端进行轮询
2、客户端使用多进程,一个进程负责发送,一个进程负责接收
3、不同程序之间使用消息队列进行通信,所有消息先发送到服务器在服务器转化成对方消息队列的类型在发送给对方
#OVER#
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
![aafe2b3b27a50eec63e98217c1f00c86.png](https://img-blog.csdnimg.cn/img_convert/aafe2b3b27a50eec63e98217c1f00c86.png)
#05#结尾->获取方式
![f0a4a4562bfcd5dbc736297f77b3e904.png](https://img-blog.csdnimg.cn/img_convert/f0a4a4562bfcd5dbc736297f77b3e904.png)
下载地址:
公众号内回复【cs1】即可获取下载链接
#OVER#
点击下方图片进入linux讨论区
●
●
●
视频来源:科学大世界
文字:科学大世界
图片:科学大世界
文章封面图:网络
如有侵权请到公众号内意见反馈处反馈
???
![f3f4cf0a7ea8716807be5d67e428c852.png](https://img-blog.csdnimg.cn/img_convert/f3f4cf0a7ea8716807be5d67e428c852.png)
扫码关注我们
觉得不错点个在看呗
2020/07/18