当前位置:网站首页>Message queue for interprocess communication
Message queue for interprocess communication
2022-07-03 08:31:00 【Spring and autumn sword armor Li Chungang】
establish server.c and client.c, The client is responsible for entering two operands and an operator , The server is responsible for receiving and calculating the two operands according to the operator , Finally, the operation result is returned to the client for output . Such as client input 2+4, The server will send the result 6 Return to the client .
Their thinking :
The purpose of this topic is to investigate how to use the related functions of message queue. The client and server can be understood as the sender and receiver respectively, so we only need to create a message queue , Every time the client passes into the message queue 3 It's worth , They are calculated numbers and operation symbols , The server only needs to process and convert the received value , I am dealing with this problem is , There is not much research on the incoming information of message queue , Instead, I think the main problem is how to convert the received message into operands and symbols .
tips: The code is rough , But the basic functions can be realized . If you have more ideas , You can write in the comments section . Accept your correction and criticism .
Code implementation
client
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<time.h>
#include<sys/types.h>
struct msgbuf
{
long mtype;
char mtext[500];
};
int main()
{
int msqid;
int msqid1;
int i;
struct msgbuf buf;
int flag;
int sendlength;
msqid = msgget((key_t)1111 , IPC_CREAT|0666);
if(msqid<0)
printf("ERROR");
buf.mtype = 1;
sendlength = sizeof(struct msgbuf) - sizeof(long);
sprintf(buf.mtext,"Hello World");
msgsnd(msqid,&buf,sendlength,0);
msgrcv(msqid,&buf,sendlength,1,0);
printf("%s",buf.mtext);
for(i = 0 ; i<3;i++)
{
scanf("%s",buf.mtext);
flag = msgsnd(msqid,&buf,sendlength,0);
if(i == 3)
break;
}
return 0;
}
The server
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<time.h>
#include<sys/types.h>
struct msgbuf
{
long mtype;
char mtext[500];
};
int main()
{
int msqid;
int msqid1;
struct msgbuf buf;
int flag;
int number1,number2,sum,i;
char *a;
int recvlength;
msqid = msgget(1111 , 0);
if(msqid<0)
printf("ERROR");
buf.mtype = 1;
recvlength = sizeof(struct msgbuf) - sizeof(long);
for(i=0;i<3;i++)
{
flag = msgrcv(msqid,&buf,recvlength,1,0);
if(i==0)
{
number1 = atoi(buf.mtext);
printf("%d\n",number1);
}
else if(i==1)
{
number2 = atoi(buf.mtext);
printf("%d\n",number2);
}
else if(i == 2)
{
a = buf.mtext;
printf("%s\n",a);
}
}
if(strcmp(a, "*") == 0)
printf("%d\n",i = number1*number2);
else if(strcmp(a, "/") == 0)
printf("%d\n",i=number1/number2);
else if(strcmp(a, "+") == 0)
printf("%d\n",i=number1+number2);
else if(strcmp(a, "-") == 0)
printf("%d\n",i=number1-number2);
else
printf("ERROR!!!\n");
sprintf(buf.mtext,"%d",i);
msgsnd(msqid,&buf,recvlength,0);
printf("OVER\n");
return 0;
}
Run a screenshot
边栏推荐
猜你喜欢
Dealing with duplicate data in Excel with xlwings
Redis的数据结构
P1596 [USACO10OCT]Lake Counting S
About Wireshark's unsuccessful installation of npcap
Unity editor expansion - draw lines
matlab神经网络所有传递函数(激活函数)公式详解
Gradle's method of dynamically modifying APK package name
Markdown learning
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
【云原生】微服务之Feign的介绍与使用
随机推荐
Creation and content of mapnode -- osgearth rendering engine series (2)
VIM learning notes from introduction to silk skating
Abstract classes and interfaces
Intersectionpicker in osgearth
Get to know unity2 for the first time
Golang中删除字符串的最后一个字符
十六进制编码简介
Go resolve ID card
Unity4.3.1 engine source code compilation process
Golang url的编码和解码
Golang的range
UE4 call DLL
C course design employee information management system
Golang 时间格式整理
【音视频】ijkplayer错误码
Golang string segmentation, substitution and interception
Notes on understanding applets 2022/7/3
MySQL containerization (1) docker installation MySQL
796 · unlock
UE4 source code reading_ Bone model and animation system_ Animation compression