//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