我想在Qt中编写一个简单的客户端服务器程序服务器是多线程的,但是因为我是Qt的新手,我在多线程中遇到问题而且我得到以下错误:没有'使我的服务器无效: :传入连接"(qintptr)"在类" myserver"中声明的成员函数。 能不能请你帮忙,如果有可能说你有更好的解决方案来编写多线程服务器吗? 这是我的代码:
" myserver.cpp:"
#include "myserver.h"
#include "mythread.h"
myserver::myserver(QObject *parent) :
QObject(parent)
{
}
void myserver::startserver()
{
int port = 1234;
if(s_server.listen(QHostAddress::Any, port))
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "Listening to port " ;
}
}
void myserver::incomingConnections(qintptr socketDescriptor)
{
mythread *thread = new mythread(socketDescriptor,this);
qDebug() << socketDescriptor << " Connecting...";
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
void myserver::acceptConnection()
{
c_client = s_server.nextPendingConnection();
connect(c_client,SIGNAL(readyRead()),this, SLOT(startRead()));
qDebug() << " Connecting...";
}
&#34; mythread.cpp:&#34;
#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(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << socketDescriptor << " Client connected";
exec();
}
void mythread::readyRead()
{
QByteArray Data = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void mythread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
答案 0 :(得分:0)
您是否将实施方法拼错为:
incomingConnections
当您在标题中将其声明为:
时incomingConnection
答案 1 :(得分:0)
检查传入连接拼写和大写以及小写字母组合,如果声明incomingComing,那么它应该是incomingConnection或者如果是incomingconnection则是incomingconnection