gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:08:21 24 4
gpt4 key购买 nike

我对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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com