命名管道通信

建立一个命名管道(文件),客户端像管道写入消息,服务端从管道读取消息。

客户端

//
//  main.cpp
//  Client
//
//  Created by 蓝猫 on 2018/11/16.
//  Copyright © 2018年 蓝猫. All rights reserved.
//

#include <iostream>
#include <stdlib.h>
#include <memory>
#include <string>
#define F_NAME "/Users/w/Desktop/unix_c/FIFO.txt"
using namespace std;
class Client
{
private:
    FILE *fp;
public:
    Client();
    ~Client(){cout<<"函数吸?"<<endl;};
    void SendStr();
};
Client::Client()
{
    fp=fopen(F_NAME, "w");
    if(fp==NULL)
    {
        cout<<"文件打开失败"<<endl;
        exit(1);
    }
    else
    {
        cout<<"文件打开成功"<<endl;
    }
};
void Client::SendStr()
{
    char *s="hello";
    cout<<"请在客户端输入你要发送到消息"<<endl;
    //scanf("%s",s);
    string temp;
    cin>>temp;
    s=(char*)temp.data();//string和char*转换
    if(temp=="quit")
    {
        fclose(fp);
        exit(1);
    }
    cout<<s<<endl;
    
    if(fputs(s,fp)==EOF)
    {
        cout<<"写fifo失败"<<endl;
        exit(1);
    }
   //fclose(fp);
};
int main(int argc, const char * argv[])
{
    cout<<"客户端发送程序"<<endl;
    shared_ptr<Client> ser=make_shared<Client>();
    while(1)
    {
        ser->SendStr();
    }
    
    return 0;
}

服务器端

//
//  main.cpp
//  Server
//
//  Created by 蓝猫 on 2018/11/16.
//  Copyright © 2018年 蓝猫. All rights reserved.
//

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <memory>
#define F_NAME "/Users/w/Desktop/unix_c/FIFO.txt"
#define MAX 80
using namespace std;
class Server
{
private:
    FILE *fp;//命名管道
    char *readbuffer;// 读缓冲区
    string temp;//暂存到上一个字符串
public:
    Server();
    ~Server();
    void GetStr();
};
Server::Server()
{
    temp="";
    readbuffer=new char[MAX];
    fp=fopen(F_NAME, "r");//打开文件 读的方式
    cout<<"hello"<<endl;
    if((fp)==NULL)//管道不存在重新创建
    {
        cout<<"文件打开失败"<<endl;
        //设置默认权限 确保你创建的文件具有所希望的缺省权限,防止其他非同组用户对你的文件具有写权限
        umask(0);
        //生成设备节点/文件
        mknod(F_NAME, S_IFIFO|0666, 0);
    }
    else
    {
        cout<<"文件打开成功"<<endl;
        fclose(fp);
    }
}
Server::~Server()
{
    delete readbuffer;
    readbuffer=NULL;
}
void Server::GetStr()
{
    int a=0;
    while (a==0)
    {
        a++;
        //cout<<"getstr"<<endl;
        // 查看文件是否打开
        if((fp=fopen(F_NAME, "r"))==NULL)
        {
            cout<<"fifo文件打开失败"<<endl;
            exit(0);
        }
        //从命名管道读取数据
        if(fgets(readbuffer,MAX,fp)!=NULL)
        {
            string s;
            s=readbuffer;
            if(s!=temp)
            {
                cout<<"接受到字符串:"<<s<<endl;
                temp=s;
            }
            
            fclose(fp);
        }
        else
        {
            if(ferror(fp))
            {
                perror(" 文件读取失败\n");
                exit(1);
            }
        }
    }
}
int main(int argc, const char * argv[])
{
    cout<<"服务器端接收程序"<<endl;
    shared_ptr<Server> ser=make_shared<Server>();
    while (1)
    {
        ser->GetStr();
    }
    
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值