linxuPipe

本文介绍了一个用于Linux环境下的管道通信机制,包括服务器端和客户端的实现。通过创建、读取和写入管道,实现进程间的数据交换。代码示例展示了如何使用C/C++进行管道操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


5.linux 下管道通信
server
#ifndef _NAMEDPIPE_SERVER_H_
#define _NAMEDPIPE_SERVER_H_

class CNamedPipeServer
{

#ifndef IN
#define IN
#endif
#ifndef OUT
#define OUT
#endif

public:
 CNamedPipeServer();
 ~CNamedPipeServer();

 /*
  函数功能:创建管道
  参数说明:IN const char* pipeserver 管道名称
      IN bool bBlock = true  规定read的方式,默认值true,read将以阻塞的方式读数据,否则非阻塞
  返回值 :成功返回true,否则返回false

 */
 bool   Create(IN const char* pipeserver,IN bool bBlock = true);

 /*
  函数功能:从管道读取数据
  参数说明:OUT char* buf  输出buf的首地址
      IN  int   nlen 输出buf的最大长度
     返回值 :成功返回读取的数据的大小,失败返回0
 */
 int    Read(OUT char* buf,IN int nlen);

 /*
  函数功能:向管道写数据
  参数说明:IN const char* pipename 客户端的管道名
      IN const void* buf  输入buf的首地址
      IN  int   nlen   输入buf的有效数据长度
  返回值 :成功返回写入数据的大小,失败返回0
 */
 int    Write(IN const char* pipename,IN const void* buf,IN int nlen);

private:
 void   Close(int& nfd);
 int    m_nserverpipe;
};

#endif

#include "namedPipeServer.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

CNamedPipeServer::CNamedPipeServer()
{
 m_nserverpipe = -1;
}
CNamedPipeServer::~CNamedPipeServer()
{
 Close(m_nserverpipe);
}
void CNamedPipeServer::Close(int &nfd)
{
 if( nfd != -1)
 {
  close(nfd);
  nfd = -1;
 }
}

bool CNamedPipeServer::Create(const char* pipeserver,bool bBlock/*=true*/)
{
 if(mkfifo(pipeserver,0777) != 0 && errno != EEXIST)
  goto RET_ERROR;
 if(bBlock)
  m_nserverpipe = open(pipeserver,O_RDWR);
 else
  m_nserverpipe = open(pipeserver,O_RDWR|O_NONBLOCK);
 if (m_nserverpipe == -1)
  goto RET_ERROR;
 return true;
RET_ERROR:
 Close(m_nserverpipe);
 return false;
}

//大于0成功,否则失败
int CNamedPipeServer::Read(char* buf,int nlen)
{
 int nRet = 0;
 if (m_nserverpipe != -1 && buf != NULL)
 {
  nRet = (int)read(m_nserverpipe,buf,nlen);
 }
 return nRet;
}

//大于0成功,否则失败
int CNamedPipeServer::Write(const char* pipename,const void* buf,int nlen)
{
 int nRet = 0;
 int nfd = open(pipename,O_RDWR);
 if(nfd != -1 && buf != NULL)
 {
  nRet = (int)write(nfd,buf,nlen);
  Close(nfd);
 } 
 return nRet;
}

client:
#ifndef _NAMEPIPE_CLIENT_H_
#define _NAMEPIPE_CLIENT_H_

class CNamePipeClient
{

#ifndef IN
#define IN
#endif
#ifndef OUT
#define OUT
#endif

public:
 CNamePipeClient();
 ~CNamePipeClient();

 /*
  函数功能:初始化
  参数说明:const char* serverpipe 服务端管道名
      const char* clientpipe 客户端管道名
  返回值 :成功返回true,失败返回false;
 */
 bool  Init(IN const char* serverpipe,IN const char* clientpipe);

 /*
  函数功能:读取数据
  参数说明:char* buf      输出buf首地址
      int   nbuflen  buf的最大值
  返回值 :成功返回读取的数据大小,0失败
 */
 int   Read(OUT char* buf,IN int nbuflen);

 /*
  函数功能:读取数据
  参数说明:const void* buf    输入buf首地址
      int   nbuflen   输入buf的有效数据长度
  返回值 :成功返写入的数据大小,0失败
 */
 int   Write(IN const void* buf,IN int nbuflen);

private:

 void  Close(int &nfd);
 bool  Create(const char* clientpipe);

 int    m_fdclient;
 int    m_fdserver;
};

#endif

#include "namePipeClient.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

CNamePipeClient::CNamePipeClient()
{
 m_fdclient = -1;
 m_fdserver = -1;
}

CNamePipeClient::~CNamePipeClient()
{
 Close(m_fdserver);
 Close(m_fdclient);
}

bool CNamePipeClient::Init(const char* serverpipe,const char* clientpipe)
{
 if(serverpipe == NULL || clientpipe == NULL )
  return false;
 m_fdserver = open(serverpipe,O_RDWR);
 if(m_fdserver == -1)
  return false;

 return Create(clientpipe);
}

bool CNamePipeClient::Create(const char* clientpipe)
{
 if(mkfifo(clientpipe,0777) != 0 && errno != EEXIST)
  return false;
 m_fdclient = open(clientpipe,O_RDWR);
 if(m_fdclient == -1)
 {
  Close(m_fdserver);
  return false;
 }
 return true;
}

void CNamePipeClient::Close(int &nfd)
{
 if(nfd != -1)
 {
  close(nfd);
  nfd = -1;
 }
}

//大于0成功,否则失败
int CNamePipeClient::Read(char* buf,int nbuflen)
{
 int nRet = 0;
 if (m_fdclient != -1 && buf != NULL)
 {
  nRet = (int)read(m_fdclient,buf,nbuflen);
 }
 return nRet;
}

//大于0成功,否则失败
int CNamePipeClient::Write(const void* buf,int nbuflen)
{
 int nRet = 0 ;
 if(m_fdserver != -1 && buf != NULL)
 {
  nRet = (int)write(m_fdserver,buf,nbuflen);
 }
 return nRet;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值