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

标签 qt sockets tcp qt5 qthread

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

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

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

我一直在关注Voidrealms的YouTube教程,并且一直在实现“正确的方法”线程。但是由于 protected ,我现在无法运行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();
}

最佳答案

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

以下链接:

  • https://forum.qt.io/topic/18692/sending-and-recieving-qtcpsocket-in-different-threads/2
  • how to use QTcpSocket in multithread?
  • https://www.qtcentre.org/threads/56840-QTcpSocket-write-to-socket-from-several-threads
  • 关于qt - Qt TCP服务器在单独的线程中运行readyRead(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664425/

    相关文章:

    java - 提高简单 TCP 客户端/套接字上的数据传输速度

    android - 如何无限期地保持建立的 TCP 连接?

    tcp - 如何使用 I/O 完成端口处理重叠 I/O 的部分写入完成

    c++ - winapi - 从 QObject 派生类调用时 GetMessage() 崩溃

    c++ - GLSL 像素着色器仅运行 0 个纹理单元

    c++ - 仅双击运行时 basic_string 析构函数上的 qt C++ 异常

    java - DatagramSocket 中的 NullPointerException

    python - 在python和qt中同时下载文件

    iphone - 在window和iPad之间建立Socket连接

    java - C 套接字 - 一次随机接收多个字符串