Qt TCP服务器在单独的线程中运行readyRead()

时间:2018-10-05 11:17:05

标签: qt sockets tcp qt5 qthread

我对Qt和套接字编程都很陌生。

我正在尝试编写一个简单的TCP服务器/聊天应用程序,但是readyRead()或“接收”部分位于它自己的线程中。我使用UDP进行了此“工作”,但由于某些原因,TCP套接字无法以相同的方式工作。

我很乐意接受其他建议,因为我可能会以错误的方式解决此问题。我最终将使这个应用程序侦听来自嵌入式系统的连续输入,读取和写入命令以及从系统接收“心跳”。

我一直在关注Voidrealms的YouTube教程,并且一直在“正确地”实施线程。但是由于受保护,我现在无法运行qThread::exec()

因此,基本上我希望在不同的线程中使用senderreceiver ,以使sender不会阻止receiver和签证反之亦然。

我将仅发布TCP服务器代码和有效的UDP代码以供参考。

tcpserver.h

#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>

class TCPServer : public QObject
{
    Q_OBJECT
public:
    explicit TCPServer(QObject *parent = nullptr);
    void SendData();
    void DoSetup(QThread &cThread);
   // void readyRead();
       bool conn;

signals:

public slots:
   void newConnection();
   void readyRead();
   void DoWork();


private:
       QTcpServer *server;
       QTcpSocket *socket;
};

#endif // TCPSERVER_H

tcpserver.cpp

#include "tcpserver.h"
#include <QThread>

TCPServer::TCPServer(QObject *parent) : QObject(parent)
{
    this->conn =0;
    server = new QTcpServer(this);
    connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));

    //telnet 169.254.55.237 1234
    if(!server->listen(QHostAddress("169.254.55.237"),1234)){
        qDebug() << "Server not started";
    }
    else{
        qDebug() << "server started";

    }
}

void TCPServer::DoSetup(QThread &cThread){
    connect(&cThread,SIGNAL(started()),this,SLOT(DoWork()));
}

void TCPServer::DoWork(){
    qDebug() << "Running Thread";
    connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}


void TCPServer::SendData(){
    while(1){
        QByteArray Data;

        Data.append("Server: ");

        QTextStream s(stdin);
        QString value = s.readLine();
        Data.append(value);

        this->socket->write(Data);
        //this->socket->write("hello client\r\n");
        this->socket->flush();
        this->socket->waitForBytesWritten(3000);
    }
}

void TCPServer::newConnection(){

    this->socket = server->nextPendingConnection();
    //connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    this->conn =1;
    this->SendData();

   //this->socket->close();
}

void TCPServer::readyRead(){
    // while(1){
    if(this->conn){
        qDebug()<< "Reading...";
        qDebug()<< this->socket->readAll();
}
}

main.cpp

#include <QCoreApplication>
#include "tcpserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QThread cThread;
    TCPServer mServer;

    mServer.DoSetup(cThread);
    mServer.moveToThread(&cThread);

    cThread.start();

    return a.exec();
}

工作UART。 myudp.h

// myudp.h

#ifndef MYUDP_H
#define MYUDP_H

#include <QObject>
#include <QUdpSocket>
#include <QDebug>
#include <QThread>

class MyUDP : public QObject
{
    Q_OBJECT
public:
    explicit MyUDP(QObject *parent = nullptr);
    void HelloUDP();
    void DoSetup(QThread &cThread);
    //void readyRead();

signals:

public slots:
    void readyRead();
    void DoWork();

private:
    QUdpSocket *socket;

};

#endif // MYUDP_H

myudp.cpp

// myudp.cpp

#include "myudp.h"

MyUDP::MyUDP(QObject *parent) :
    QObject(parent)
{
    // create a QUDP socket
    socket = new QUdpSocket(this);

    socket->bind(QHostAddress(" 169.254.55.237"), 1234);

    //connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void MyUDP::DoSetup(QThread &cThread){
    connect(&cThread,SIGNAL(started()),this,SLOT(DoWork()));
}

void MyUDP::DoWork(){
qDebug() << "Running Thread";
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void MyUDP::HelloUDP()
{
    while(1){
    QByteArray Data;
    Data.append("Server: ");

    QTextStream s(stdin);
    QString value = s.readLine();
    Data.append(value);

    socket->writeDatagram(Data, QHostAddress("169.254.55.237"), 4321);
    //socket->writeDatagram(Data, QHostAddress("127.0.0.1"), 1234);
}
}

void MyUDP::readyRead()
{
   // if(socket->hasPendingDatagrams()){
    // when data comes in
    QByteArray buffer;
    buffer.resize(socket->pendingDatagramSize());

    QHostAddress sender;
    quint16 senderPort;

    socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);

    qDebug() << "Message from: " << sender.toString();
    qDebug() << "Message port: " << senderPort;
    qDebug() << "Message: " << buffer;

   // }
}

main.cpp

#include <QCoreApplication>
#include <QThread>
#include "myudp.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << "Server";

    QThread cThread;

    MyUDP client;

    client.DoSetup(cThread);
    client.moveToThread(&cThread);

    cThread.start();

    client.HelloUDP();

    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

在@thuga评论表和一些研究之后,在我看来,不可能(或不建议)在单独的线程中具有读写功能。 QTcpSocket不是线程安全的。似乎最好的解决方案是在单独的线程中创建辅助函数,这些函数会发出信号以使主线程(或带有QTCPSocket的线程)执行操作。例如,可以将一个用于读取用户输入的辅助函数放在一个新线程中,并且当有新数据时,可以发出一个信号,指示QTCPSocket可以发送该信号。

QTCPSocket的线程似乎更多地用于在其自己的线程中处理多个连接。

下面的链接: