Qt 5.6 使用 QLocalServer 和 QLocalSocket 传递文件描述符
Qt 5.6 Passing File Descriptors with QLocalServer and QLocalSocket
我正在学习如何在 Ubuntu 14.04 上使用带有 Qt 5.6 的 Unix 域套接字传递文件描述符。从文档中可以看出,执行此操作的方法是使用 QLocalServer 和 QLocalSocket classes。
我创建了 2 个简单的应用程序,1 个服务器和 1 个客户端。它们每个只有 1 个 class,我已将整个 class 粘贴在下面。
我可以接收整数形式的 'file descriptor',但是当我尝试以追加模式打开它以便在客户端进程中写入时,出现以下错误:
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 上,使用类型 SCM_RIGHTS
的辅助数据使用 Unix 域套接字发送文件描述符。 Qt好像不支持使用QLocalSocket
和QLocalServer
这样的数据,有个老QTBUG在讲这个问题。而且好像还没解决
你可以使用 Qt D-Bus where you can send file descriptors using QDBusUnixFileDescriptor class.Or I'm afraid you may have to use Unix domain sockets to implement that yourself, there is a good example here.
但是您确定您真的需要在应用程序之间传递文件描述符吗?对于学习以外的目的,我认为总有办法克服这个限制。
P.S。您正在使用 QDataStream
序列化套接字文件描述符(就好像它是一个普通的 int
)然后发送序列化数据以供其他进程也像普通的 int
一样读取它。这将允许您读取文件描述符的整数值,但您 不会 允许您与它进行任何交互,因为文件描述符值在它们所属的进程之外没有意义。
我正在学习如何在 Ubuntu 14.04 上使用带有 Qt 5.6 的 Unix 域套接字传递文件描述符。从文档中可以看出,执行此操作的方法是使用 QLocalServer 和 QLocalSocket classes。
我创建了 2 个简单的应用程序,1 个服务器和 1 个客户端。它们每个只有 1 个 class,我已将整个 class 粘贴在下面。
我可以接收整数形式的 'file descriptor',但是当我尝试以追加模式打开它以便在客户端进程中写入时,出现以下错误:
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 上,使用类型 SCM_RIGHTS
的辅助数据使用 Unix 域套接字发送文件描述符。 Qt好像不支持使用QLocalSocket
和QLocalServer
这样的数据,有个老QTBUG在讲这个问题。而且好像还没解决
你可以使用 Qt D-Bus where you can send file descriptors using QDBusUnixFileDescriptor class.Or I'm afraid you may have to use Unix domain sockets to implement that yourself, there is a good example here.
但是您确定您真的需要在应用程序之间传递文件描述符吗?对于学习以外的目的,我认为总有办法克服这个限制。
P.S。您正在使用 QDataStream
序列化套接字文件描述符(就好像它是一个普通的 int
)然后发送序列化数据以供其他进程也像普通的 int
一样读取它。这将允许您读取文件描述符的整数值,但您 不会 允许您与它进行任何交互,因为文件描述符值在它们所属的进程之外没有意义。