从QThread调用QTcpSocket类的write函数时出现错误

时间:2016-07-19 03:39:05

标签: multithreading qt qtcpsocket qtcpserver

我正在学习Qt中的多线程编程,然后我从QTcpSocket调用QThread类的write函数。函数输出为:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0x6f1840), parent's thread is QThread(0x624e90), current thread is QThread(0x716ed0)

我称之为写函数的代码:

QString Processor::GetSystemInfoOfClient() const
{
    QString result;
    const char *send_buffer = "GSIOC";
    char receive_buffer[100];
    this->cli_sock->write(send_buffer);
    this->cli_sock->waitForBytesWritten();
    this->cli_sock->waitForReadyRead(100000);
    this->cli_sock->read(receive_buffer, 100);
    result = QString(receive_buffer);
    return result;
}

我接受连接的代码:

void Processor::Accept()
{
    this->cli_sock = m_server->nextPendingConnection();
    this->cli_addr = this->cli_sock->peerAddress();
    this->cli_port = this->cli_sock->peerPort();
}

连接处理器的定义:

class Processor : public QObject
{
    Q_OBJECT
public:
    explicit Processor(QObject *parent = 0, QTcpServer *server = nullptr);
private:
    QTcpServer *m_server = nullptr;
    QTcpSocket *cli_sock = nullptr;
    QHostAddress cli_addr;
    quint16 cli_port;

    QString GetSystemInfoOfClient() const;
signals:
   void GetDetailedFinished(const QString &address,const QString &port, const QString &SystemInfo);
public slots:
   void GetClientDetail();
   void Accept();
};

然后我将处理器移到QThread

    Processor *processor = new Processor(0, server);
    processor->moveToThread(&processor_thread);
    connect(&processor_thread, &QThread::finished, processor, &QObject::deleteLater);
    connect(this, &Processor_Controller::Accept, processor, &Processor::Accept);
    connect(this, &Processor_Controller::GetClientDetail, processor, &Processor::GetClientDetail);
    connect(processor, &Processor::GetDetailedFinished, this, &Processor_Controller::PassClientDetail);
    processor_thread.start();

我在主线程中创建了QTcpServer

    // The address of the server
    QHostAddress srv_addr("127.0.0.1");

    // The server listen port
    quint16 srv_port = 9895;

    Processor_Controller *controller = new Processor_Controller(0, &server);

    connect(&server, &QTcpServer::newConnection, controller, &Processor_Controller::BeginProcess);

    connect(controller, &Processor_Controller::DisplayClientDetail, this, &MainWindow::DisplayClientDetail);

    // BeginListen
    if(!server.listen(srv_addr, srv_port))
        emit statusBar()->showMessage("Listen Failed");
    else
        emit statusBar()->showMessage("Listen Success");

调用GetSystemInfoOfClient函数的函数:

void Processor::GetClientDetail()
{
    QString address    = this->cli_addr.toString();
    QString port       = QString::number(this->cli_port);
    QString SystemInfo = this->GetSystemInfoOfClient();
    emit this->GetDetailedFinished(address, port, SystemInfo);
}

处理器的构造函数:

Processor::Processor(QObject *parent, QTcpServer *server) : QObject(parent)
{
    this->m_server = server;
}

我只想在主线程中创建一个服务器并处理Processor类中的连接。如何在不改变这种想法的情况下避免这种错误?

1 个答案:

答案 0 :(得分:1)

我在Processor::Accept函数

中使用这两行代码解决了这个问题
QTcpSocket *m_socket = m_server->nextPendingConnection();
this->cli_sock->setSocketDescriptor(m_socket->socketDescriptor(), m_socket->state(), m_socket->openMode());