c++发送
#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
using namespace std;
int communication(void);
int main(void)
{
char data[]="dssaaaaaa";
int pipe_fd_wr;
pipe_fd_wr = communication();
while(1){
write(pipe_fd_wr,data,5);
}
}
int communication(void){
const char *fifo_name = "../CppToPython";
int res =0;
const int open_mode = O_WRONLY;
int pipe_fd_wr;
if(access(fifo_name,F_OK)==-1)
{
res = mkfifo(fifo_name,0777);
if(res!=0)
{
fprintf(stderr,"Could not create fifo %s\n",fifo_name);
exit(EXIT_FAILURE);
}
}
//以只写阻塞方式打开FIFO文件
pipe_fd_wr = open(fifo_name, open_mode);
if(pipe_fd_wr==-1){
cout<< " open CppToPython err!"<<endl;
}
else{
cout<< " open CppToPython successfully!"<<endl;
}
return pipe_fd_wr;
}
python接收
import os
import json
from time import sleep
def openFIFO():
get_info_path = "../CppToPython"
counter = 1
f = os.open(get_info_path, os.O_RDONLY)
print("Client open f", f)
return f
def getData(f):
data = os.read(f,5)
return data
def closeFIFO(f):
os.close(f)
if __name__ == "__main__":
f = openFIFO()
while 1:
data = getData(f)
sleep(1)
print(data)
closeFIFO(f)
阻塞读