我正在編寫簡單的客戶端服務器程序在Qt中,服務器是多線程。 對於一個服務器它工作正常,它可以發送消息從客戶端到服務器,但在多線程形式它不起作用,它連接到線程,並顯示消息「客戶端連接」,但它不能顯示從客戶端發送的消息! 我搜查了很多芽我找不到什麼問題和解決方案..這是我的代碼: 你能幫我。提前致謝。多線程服務器不工作
myserver.cpp
#include "myserver.h"
#include "mythread.h"
myserver::myserver(QObject * parent): QTcpServer(parent)
{
}
void myserver::startserver()
{
int port = 6666;
if (!this - > listen(QHostAddress::Any, port)) {
qDebug() << "Could not start server ";
} else {
qDebug() << "Listening to port ";
}
}
void myserver::incomingConnection(qintptr socketDescriptor)
{
qDebug() << socketDescriptor << " Connecting...";
mythread * thread = new mythread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread - > start();
}
mythread.cpp
#include "mythread.h"
#include "myserver.h"
mythread::mythread(qintptr ID, QObject *parent) : QThread(parent)
{
this->socketDescriptor = ID;
}
void mythread::run()
{
qDebug() << " Thread started";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor)) {
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
qDebug() << socketDescriptor << " Client connected";
}
void mythread::readSocket()
{
QByteArray Data = socket->readAll();
qDebug()<< socketDescriptor <<" Data in: " << Data;
socket->write(Data);
}
偏離主題。我們不會調試你的代碼或做你的功課。你是否編譯過所有警告和調試信息('g ++ -Wall -Wextra -g')然後**使用調試器**('gdb')? –