我想使用FIFO(IPC)在两个应用程序之间进行通信,我创建了一个由" mkfifo mypipe"命令的FIFO。
第一个应用程序:
....
....
fd = open("MyPipe", O_NONBLOCK | O_WRONLY);
QByteArray buf;
QDataStream bdwr(&buf,QIODevice::WriteOnly);
bdwr.setVersion(kDSVersion);
myclassObjWr.lname = "AAAAA";
myclassObjWr.fname = "BBBBB";
bdwr << myclassObjWr.lname << myclassObjWr.fname ;
ssize_t written = write(fd,buf.data() , buf.length());
.....
.....
第二个应用程序:
....
....
fd = open("MyPipe", O_NONBLOCK | O_RDONLY);
ssize_t nread;
QByteArray rxbuf;
static const QDataStream::Version kDSVersion = QDataStream::Qt_5_12;
QDataStream bdrd(&rxbuf, QIODevice::ReadWrite);
bdrd.setVersion(kDSVersion);
nread = read(fd, rxbuf.data(),200);
if ( nread > 0)
{
bdrd.writeRawData(rxbuf.data(),nread);
bdrd >> myclassObjrd.lname >> myclassObjrd.fname;
qDebug() << "lname: " << myclassObjrd.lname;
}
....
....
阅读IPC FIFO后," rxbuf"已完全初始化,但" myclassobjrd.lname"one_answers" myclassobjrd.fname"都是空的,并且不初始化。
它是通过使用中间的无符号char缓冲区来求解的
# ...
ssize_t nread;
QByteArray buf;
unsigned char* rxbuf;
static const QDataStream::Version kDSVersion = QDataStream::Qt_5_12;
QDataStream bdrd(&buf, QIODevice::ReadOnly);
bdrd.setVersion(kDSVersion);
rxbuf = (unsigned char*)malloc(200);
nread = read(fd, rxbuf,200);
if ( nread > 0)
{
for (int i = 0 ; i < nread; i++) {
buf[i] = rxbuf[i] ; //init QByteArray
}
bdrd >> myclassObjrd.lname >> myclassObjrd.fname;
qDebug() << "lname: " << myclassObjrd.lname;
}
# ...