服务器和客户机实现通讯

用命名管道分别写一个服务器程序和客户机程序,客户机的的父进程负责每隔一秒产成一个子进程,(形成一个进程扇),而每个子进程则往自己的FIFO中写入自己的PID号码,服务器负责从FIFO中读取数据并打印在屏幕上。

要求:

用open函数处理重复创建问题

使用access函数处理重复创建问题

//客户机实例程序

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <strings.h>


#define S2C 10
#define C2S 20

struct msgbuf{
    long mtype;    //类型
    char mtext[50];//正文
};

void func(int sig)
{
    exit(0);
}

int main(int argc,char *argv[])
{
    //1. 为消息队列申请key值与ID号
    key_t key = ftok(".",10);
    int msgid = msgget(key,IPC_CREAT|0666);
    
    //2. 带着这条队列去产生一个小孩
    pid_t x;
    x = fork();
    if(x > 0)  //父进程  发送消息
    {
        signal(SIGCHLD,func);
        
        struct msgbuf gec;
        int ret;
        while(1)
        {
            bzero(&gec,sizeof(gec));
            gec.mtype = C2S;
            fgets(gec.mtext,sizeof(gec.mtext),stdin);
            ret = msgsnd(msgid,&gec,strlen(gec.mtext),0);
            if(ret == -1)
            {
                printf("msgsnd error!\n");
            }
            
            if(strncmp(gec.mtext,"quit",4) == 0)
            {
                kill(x,SIGUSR1);
                exit(0);
            }
        }
    }
    
    if(x == 0)  //子进程   接收消息
    {
        signal(SIGUSR1,func);
        
        struct msgbuf gec;
        int ret;
        while(1)
        {
            bzero(&gec,sizeof(gec));
            ret = msgrcv(msgid,&gec,sizeof(gec.mtext),S2C,0);
            if(ret == -1)
            {
                printf("msgrcv error!\n");
            }
            
            printf("from server:%s",gec.mtext);
            
            if(strncmp(gec.mtext,"quit",4) == 0)
            {
                msgctl(msgid,IPC_RMID,NULL); //删除消息队列
                exit(0); //子进程    会自动发送SIGCHLD给父进程。
            }
        }
    }
}

//服务器示例程序

#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <strings.h>


#define S2C 10
#define C2S 20

struct msgbuf{
    long mtype;    //类型
    char mtext[50];//正文
};

void func(int sig)
{
    exit(0);
}

int main(int argc,char *argv[])
{
    //1. 为消息队列申请key值与ID号
    key_t key = ftok(".",10);
    int msgid = msgget(key,IPC_CREAT|0666);
    
    //2. 带着这条队列去产生一个小孩
    pid_t x;
    x = fork();
    if(x > 0)  //父进程  发送消息
    {
        signal(SIGCHLD,func);
        
        struct msgbuf gec;
        int ret;
        while(1)
        {
            bzero(&gec,sizeof(gec));
            gec.mtype = S2C;
            fgets(gec.mtext,sizeof(gec.mtext),stdin);
            ret = msgsnd(msgid,&gec,strlen(gec.mtext),0);
            if(ret == -1)
            {
                printf("msgsnd error!\n");
            }
            
            if(strncmp(gec.mtext,"quit",4) == 0)
            {
                kill(x,SIGUSR1);
                exit(0);
            }
        }
    }
    
    if(x == 0)  //子进程   接收消息
    {
        signal(SIGUSR1,func);
        
        struct msgbuf gec;
        int ret;
        while(1)
        {
            bzero(&gec,sizeof(gec));
            ret = msgrcv(msgid,&gec,sizeof(gec.mtext),C2S,0);
            if(ret == -1)
            {
                printf("msgrcv error!\n");
            }
            
            printf("from client:%s",gec.mtext);
            
            if(strncmp(gec.mtext,"quit",4) == 0)
            {
                msgctl(msgid,IPC_RMID,NULL); //删除消息队列
                exit(0); //子进程    会自动发送SIGCHLD给父进程。
            }
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肖爱Kun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值