我正在学习如何在Ubuntu 14.04上使用带有Qt 5.6的Unix域套接字传递文件描述符。从文档中看,实现这一点的方法似乎是使用QLocalServer和QLocalSocket类。
我创建了两个简单的应用程序,一个服务器和一个客户端。他们每个人只有一个类,我把所有的类都粘贴在下面。
我可以接收整数形式的"文件描述符",但当我尝试以附加模式打开它以便在客户端进程中写入时,我会收到以下错误:
Cannot open existing file handle: "Unknown error"
QIODevice::write: device not open
FD Received: 20
我做错了什么?
客户:
#include <QtWidgets>
#include <QtNetwork>
#include "localclient.h"
Client::Client(QObject *parent)
{
socket = new QLocalSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readFd()));
connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(displayError(QLocalSocket::LocalSocketError)));
requestFd();
}
void Client::requestFd()
{
socket->abort();
socket->connectToServer("mysocket");
}
void Client::readFd()
{
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
if (socket->bytesAvailable() < (int)sizeof(quint16))
return;
if (in.atEnd())
return;
int nextFd;
in >> nextFd;
QFile rxFile;
if ( !rxFile.open(nextFd,QIODevice::Append) ) {
qDebug() << "Cannot open existing file handle: " << rxFile.errorString();
}
rxFile.write("hello");
qDebug() << "FD Received: " << nextFd;
}
void Client::displayError(QLocalSocket::LocalSocketError socketError)
{
switch (socketError) {
case QLocalSocket::ServerNotFoundError:
tr("The host was not found. Please check the "
"host name and port settings.");
break;
case QLocalSocket::ConnectionRefusedError:
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct.");
break;
case QLocalSocket::PeerClosedError:
break;
default:
tr("The following error occurred: %1.").arg(socket->errorString());
}
}
服务器:
#include <QtWidgets>
#include <QtNetwork>
#include <stdlib.h>
#include "localserver.h"
#include <qlocalserver.h>
#include <qlocalsocket.h>
Server::Server(QObject *parent)
{
server = new QLocalServer(this);
if (!server->listen("mysocket")) {
qDebug() << QString("Unable to start the server: %1.").arg(server->errorString());
return;
}
qDebug() << tr("The server is running");
connect(server, SIGNAL(newConnection()), this, SLOT(sendFd()));
fileToSend = new QFile("/home/me/Desktop/test.bin");
if ( !fileToSend->open(QIODevice::WriteOnly) ) {
qDebug() << "Unable to open file to send";
}
}
void Server::sendFd()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out.device()->seek(0);
qDebug() << "Sending this file handle: " << fileToSend->handle();
out << fileToSend->handle();
QLocalSocket *clientConnection = server->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
clientConnection->write(block);
clientConnection->flush();
clientConnection->disconnectFromServer();
}
在linux上,使用Unix域套接字发送文件描述符是使用类型为SCM_RIGHTS
的辅助数据完成的。Qt似乎不支持使用QLocalSocket
和QLocalServer
的数据,有一个老的QTBUG在谈论这个问题。而且这个问题似乎还没有解决。
您可以使用QtD-Bus,在那里您可以使用QDBusUnixFileDescriptor类发送文件描述符。或者我担心你可能不得不使用Unix域套接字来自己实现,这里有一个很好的例子。
但是你确定你真的需要在应用程序之间传递文件描述符吗?除了学习目的之外,我认为总有办法克服这种限制。
附言:您正在使用QDataStream
序列化套接字文件描述符(就像它是一个普通的int
一样),然后发送序列化的数据给另一个进程,使其也作为一个普通int
读取。这将允许您读取文件描述符的整数值,但您将不允许您与它进行任何交互,因为文件描述符的值在它们所属的进程之外没有意义。