从 Qt 上的 UNIX 域套接字 (QLocalSocket) 读取

Reading from a UNIX domain socket (QLocalSocket) on Qt

我在从 UNIX 域套接字读取时遇到问题,服务器应用程序是基于 Python 的,我需要实现 IPC 才能与作为客户端的 Qt 应用程序通信。服务器应用程序已经过测试,可以与 Python 客户端应用程序一起使用。这是我在 Qt 客户端上的内容:

unixsocket.cpp

#include "unixsocket.h"
#include <stdio.h>

UnixSocket::UnixSocket(QObject *parent) :
    QObject(parent)
{
    this->socket = new QLocalSocket (this);
}

UnixSocket::~UnixSocket(){}

void UnixSocket::Test ()
{
    connect (this->socket, SIGNAL(connected()), this, SLOT(connected_callback()));
    connect (this->socket, SIGNAL(disconnected()), this, SLOT(disconnected_callback()));
    connect (this->socket, SIGNAL(readyRead()), this, SLOT(readyRead_callback()));
    connect (this->socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten_callback(qint64)));

    qDebug() << "Connecting...";

    socket->connectToServer(ADAPTER_SOCKET);

    if (!socket->waitForConnected(1000))
    {
        qDebug() << "Error: " << this->socket->errorString();
    }
    else
    {
        in.setDevice(socket);
        in.setVersion(QDataStream::Qt_5_8);
        this->blockSize = 0;

    }

}

void UnixSocket::connected_callback()
{
    qDebug() << "Connected to " << socket->fullServerName();

}

void UnixSocket::disconnected_callback()
{
    qDebug() << "Disconnected!";
}

void UnixSocket::bytesWritten_callback(qint64 bytes)
{
    qDebug() << "We wrote: " << bytes;
}

void UnixSocket::readyRead_callback()
{
    qDebug() << "Reading...";
    qDebug() << "Block Size: " << this->blockSize;

    if (this->blockSize == 0) {
        // Relies on the fact that QDataStream serializes a quint32 into
        // sizeof(quint32) bytes
        if (socket->bytesAvailable() < (int)sizeof(quint32))
            return;
        in >> this->blockSize;
    }

    if (socket->bytesAvailable() < this->blockSize || in.atEnd())
        return;

    QString recvd_data;
    in >> recvd_data;
    qDebug() << recvd_data;
}

unixsocket.h

#ifndef UNIXSOCKET_H
#define UNIXSOCKET_H

#include <QObject>
#include <QAbstractSocket>
#include <QLocalSocket>
#include <QtNetwork>
#include <QIODevice>
#include <QDebug>
#include <QTextStream>
#include <QDataStream>

#define ADAPTER_SOCKET "/opt/adapter/socket"


class UnixSocket: public QObject
{
    Q_OBJECT

public:
    explicit UnixSocket(QObject *parent = 0);
    ~UnixSocket();
    void Test ();

signals:

public slots:

    void connected_callback();
    void disconnected_callback();
    void bytesWritten_callback (qint64 bytes);
    void readyRead_callback();

private:
    QLocalSocket *socket;
    QDataStream in;
    quint32 blockSize;
};

#endif // UNIXSOCKET_H

main.cpp触发class:

int main(int argc, char* argv[])
{
    UnixSocket mUnixSocket;
    mUnixSocket.Test();
    while (1);
    return 1;
}

现在,Unix 域套接字连接工作正常。所以,我收到 "connected" 消息。而且我确定服务器正在异步发送一些数据,但由于某种原因我的程序根本没有输入 readyRead_callback 。我需要有关如何解决此问题的指示。非常感谢任何帮助。

需要存在于 Qt 事件循环中,而在你的情况下你没有,所以它对你不起作用,所以将你的代码更改为:

int main(int argc, char *argv[]) { 
    QCoreApplication a(argc, argv);
    UnixSocket mUnixSocket; 
    mUnixSocket.Test(); 
    return a.exec(); 
}

如何在服务器中监听unix domain socket? 使用 QLocalServer,但未调用 onNewConnection

mServer = new QLocalServer;
connect(mServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));