linux--fifo(单个服务器,多个客户端实例)
来源:互联网 发布:protobuf golang grpc 编辑:程序博客网 时间:2024/04/29 16:39
//client
#include <iostream>
#include "App.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "string.h"
#define FIFO "/home/flc/srvfifo.tmp"
using namespace std;
int main() {
Ptrol senp, secvp;
char buff[1028];
char recvbuff[1028];
memset(buff, 0, sizeof(buff));
memset(recvbuff, 0, sizeof(recvbuff));
senp.Pid = getpid();
printf("client prosses id is %d\n", senp.Pid);
memset(senp.data, 0, sizeof(senp.data));
memcpy(senp.data, "ghl", sizeof("ghl"));
memset(senp.pathname, 0, sizeof(senp.pathname));
snprintf(senp.pathname, sizeof(senp.pathname), "/home/flc/clientfifo.%ld",
(long) senp.Pid);
unlink(senp.pathname);
if (mkfifo(senp.pathname, 0666) < 0) {
printf("client create fifoc error");
return -1;
}
printf("senp.pathname is %s\n", senp.pathname);
int fd = open(FIFO, O_WRONLY);
if (fd < 0) {
printf("client open fifo error\n");
return -1;
}
memcpy(buff, &senp, sizeof(senp));
int result = write(fd, buff, sizeof(buff));
if (result == 1028) {
int fd1 = open(senp.pathname, O_RDONLY);
if (fd1 < 0) {
printf("client open fifoc error\n");
return -1;
}
if (read(fd1, recvbuff, sizeof(recvbuff)) == 1028) {
memcpy(&secvp, recvbuff, sizeof(secvp));
printf("client recvbuff p_id %d\n", secvp.Pid);
printf("client recvbuff is %s\n", secvp.data);
close(fd1);
return 0;
}
}
return 0;
}
//server
#include <iostream>
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "string.h"
#include "App.h"
#include "ClientPthread.h"
#define FIFO "/home/flc/srvfifo.tmp"
//#define MAXLINE1 1028;
using namespace std;
int main() {
int fd;
char readbuff[1028];
memset(readbuff, 0, sizeof(readbuff));
unlink(FIFO);
//创建server-fifo
if (mkfifo(FIFO, 0666) < 0) {
printf("server create fifo error!\n");
return -1;
}
//打开serverfifo,用于读取客户端的请求
fd = open(FIFO, O_RDONLY);
if (fd < 0) {
printf("server open fifo error!\n");
return -1;
}
ClientPthread *clpth;
while(true)
{
if(read(fd,readbuff,sizeof(readbuff))>0)
{
Ptrol p;
memcpy(&p,readbuff,sizeof(p));
clpth= new ClientPthread(p);
clpth->create();
close(fd);
}
}
cout << "!!!Hello World!!!" << endl; // prints !!
//Pthread.h
#ifndef PTHREAD_H_
#define PTHREAD_H_
#include "pthread.h"
class Pthread {
public:
Pthread();
bool create();
void join();
virtual ~Pthread();
virtual void thread_work();
static void *wrapper(void *args);
private:
pthread_t pthid;
};
#endif /* PTHREAD_H_ */
//Pthread.cpp
#include "Pthread.h"
Pthread::Pthread() {
// TODO Auto-generated constructor stub
this->pthid=-1;
}
void Pthread::thread_work(){
}
void * Pthread::wrapper(void *args){
Pthread *pth=static_cast<Pthread *>(args);
pth->thread_work();
return NULL;
}
void Pthread::join(){
pthread_join(this->pthid,NULL);
}
bool Pthread::create(){
if(pthread_create(&pthid,NULL,wrapper,this)!=0){
return false;
}
return true;
}
Pthread::~Pthread() {
// TODO Auto-generated destructor stub
}
//ClientPthread.h
#ifndef CLIENTPTHREAD_H_
#define CLIENTPTHREAD_H_
#include "Pthread.h"
#include "App.h"
class ClientPthread: public Pthread {
public:
Ptrol p;
ClientPthread(Ptrol p);
void thread_work();
virtual ~ClientPthread();
};
//ClientPthread.cpp
#include "ClientPthread.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "string.h"
#include "unistd.h"
ClientPthread::ClientPthread(Ptrol p) {
// TODO Auto-generated constructor stub
this->p=p;
}
void ClientPthread::thread_work()
{
printf("client p_id is %d\n",this->p.pid);
printf("client pathname is %s\n",this->p.pathname);
int fd=open(this->p.pathname,O_WRONLY);
if(fd<0)
{
printf("server open client fifo error\n");
return ;
}
char sendbuf[1028];
memcpy(this->p.data,"recv",sizeof("recv"));
printf("server send client buff is %s\n",p.data);
memset(sendbuf,0,sizeof(sendbuf));
memcpy(sendbuf,&p,sizeof(p));
int result=write(fd,sendbuf,sizeof(sendbuf));
printf("server send client result is %d\n",result);
}
ClientPthread::~ClientPthread() {
// TODO Auto-generated destructor stub
}
//App.h
#ifndef APP_H_
#define APP_H_
#include "string.h"
#include "pthread.h"
class App {
public:
App();
virtual ~App();
};
struct Ptrol{
int pid;
char pathname[32];
char data[512];
void reset(char *buff)
{
memcpy(this,buff,strlen(buff));
}
void operator =(Ptrol p)
{
this->pid=p.pid;
memset(this->data,0,sizeof(this->data));
memset(this->pathname,0,sizeof(this->pathname));
memcpy(this->data,p.data,sizeof(p.data));
memcpy(this->pathname,p.pathname,sizeof(p.pathname));
}
};
#endif /* APP_H_ */
//编译完先执行server,然后执行client
- linux--fifo(单个服务器,多个客户端实例)
- 单个服务器对多个客户端简单通讯
- fifo实现单服务器多客户端
- Windows Socket 编程_单个服务器对多个客户端简单通讯
- Windows Socket 编程_单个服务器对多个客户端简单通讯
- Windows Socket 编程_单个服务器对多个客户端简单通讯
- 在Java中使用pscp命令上传单个或者多个文件(文件夹)到远程Linux服务器
- 使用fifo实现一个服务器端多个客户端通信
- linux 下安装subversion 客户端和服务器(实例)
- 服务器、客户端实例(C#)
- Linux下网络socket编程——实现服务器(select)与多个客户端通信
- 如何在单个Oracle实例中创建多个Geodatabase
- 如何在单个Oracle实例中创建多个Geodatabase
- 一、mongodb之单个实例、主从配置、多个节点
- Resin4.0+ 单个服务配置多个实例
- Resin4.0+ 单个服务配置多个实例
- 单个服务器和客户端的通信
- 服务器与单个客户端多次通信
- POJ 1595 素数筛法
- 黑马程序员_IO流(上)
- asp在utf-8编码时读写文件
- Android 蓝牙 资料(转)
- MYSQL LEAVE with no matching label: top
- linux--fifo(单个服务器,多个客户端实例)
- #云端对话——云的那些事#《云计算:大数据时代的系统工程》作者在中关村图书大厦和你面对面
- VIM show [Tab] [ ]
- 编程方法学——观后笔记【3】
- sql 截取字符串
- 了解你的受众 社交网站营销中的心理学
- TDS-Tabular Data Stream
- Android build/envsetup.sh 脚本分析(lunch函数)
- getOutputStream() has already been called for this response