我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来:

common.h

 #ifndef __COMMON_H_
#define __COMMON_H_ #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <string.h>
#include <time.h> #define MSG_SIZE 1024
#define FILEPATH "."
#define ID 0
#define SERVER_TYPE 1
#define CLIENT_TYPE 2 typedef struct msg_info {
long mtype;
char mtext[MSG_SIZE];
}msginfo; int CreateMessageQueue();
int GetMessageQueue();
int DeleteMessageQueue(int msgid);
int SendDataToMessageQueue(int msg_id, int send_type, char *msg);
int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out); #endif

common.c

 #include "common.h"

 static int CommonMessageQueue(int flags)
{
key_t _key = ftok(FILEPATH, ID);
if(_key == -) {
perror("ftok error");
return ;
}
int _msg_id = msgget(_key, flags);
if(_msg_id < ) {
perror("msgget error");
return ;
}
return _msg_id;
} int CreateMessageQueue()
{
return CommonMessageQueue(IPC_CREAT|IPC_EXCL|);
} int GetMessageQueue()
{
return CommonMessageQueue(IPC_CREAT);
} int DeleteMessageQueue(int msg_id)
{
if(msgctl(msg_id, IPC_RMID, NULL) < )
return -;
return ;
} int SendDataToMessageQueue(int msg_id, int send_type, char *msg)
{
msginfo buff;
buff.mtype = send_type;
strcpy(buff.mtext, msg);
int msg_snd = msgsnd(msg_id, (void *)&buff, sizeof(buff), );
if(msg_snd < ) {
perror("msgsnd error");
return -;
}
return ;
} int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out)
{
msginfo buff;
int msg_rcv = msgrcv(msg_id, (void *)&buff, sizeof(buff), receive_type, );
if(msg_rcv < ) {
perror("msg_rcv error");
return -;
}
strcpy(out, buff.mtext);
return ;
}

server.c

 #include "common.h"

 int main()
{
char buff[MSG_SIZE];
int msg_id = CreateMessageQueue(); while()
{
//send data
printf("server please enter# ");
fflush(stdout);
ssize_t s = read(, buff, sizeof(buff)-);
if(s > ) {
buff[s-] = ;
SendDataToMessageQueue(msg_id, SERVER_TYPE, buff);
printf("data has sended,wait receive......\n");
} else {
perror("read error");
return ;
} //receive data
ReceiveDataFromMessageQueue(msg_id, CLIENT_TYPE, buff);
printf("from client: %s\n", buff);
}
DeleteMessageQueue(msg_id); return ;
}

client:

 #include "common.h"

 int main()
{
char buff[MSG_SIZE];
int msg_id = GetMessageQueue();
while() {
//receive data
ReceiveDataFromMessageQueue(msg_id, SERVER_TYPE, buff);
printf("from server:%s\n", buff); //send data
printf("client please enter# ");
fflush(stdout);
ssize_t s = read(, buff, sizeof(buff)-);
if(s <= ) {
perror("read error");
return ;
} else {
buff[s-] = ;
SendDataToMessageQueue(msg_id, CLIENT_TYPE, buff);
printf("data has sended,wait receive......\n");
}
}
return ;
}

Makefile:

all:client server

client: common.c client.c
gcc -o $@ $^
server: common.c server.c
gcc -o $@ $^ .PHONY:clean
clean:
rm -rf client server

最新文章

  1. iOS 归档archive使用方法
  2. iOS基础CGAffineTransform的简单使用
  3. G - Just a Hook
  4. linux 内核之旅
  5. BlockingQueue接口
  6. jquery第二期:三个例子带你走进jquery
  7. php中的short_open_tag的作用
  8. curl file_get_contents fsockopen
  9. Node.js 基础介绍
  10. php正则表达式,在抓取内容进行匹配的时候表现不稳定
  11. yagmail让发邮件更简单
  12. Linux set命令参数及用法详解
  13. 新手必看!Office Web Apps 2013 安装与配置(实战)
  14. 初级Java工程师面试所遇面试题
  15. 普通程序员转型AI免费教程整合,零基础也可自学
  16. 打印流-PrintStream和PrintWriter
  17. Python学习之路:NumPy初识
  18. AtCoder Regular Contest 098 F.Donation
  19. 【shell 练习4】编写Shell用户管理脚本(二)
  20. [NOIP2015] D1T2 信息传递

热门文章

  1. MYSQL索引优化之单表示例
  2. SQL Server 2008性能故障排查(三)——IO
  3. 【Java】java获取request body
  4. vue中的文件上传和下载
  5. MIS(管理信息系统)
  6. APPium连接真机输入框中输入的内容与代码中不一致
  7. oracle函数自治事务解决不能增改删的语句操作
  8. 1.OpenCV数据类型
  9. 斯坦福【概率与统计】课程笔记(四):EDA | 茎叶图
  10. Cocos2d-x之Vector&lt;T&gt;