[solved]QTcpServer can't catch the SIGNAL "newConnection"
-
I run this TcpServer in a new thread, and I send a signal from main thread, it works.BUT! when a client connect to this server, the signal "newConnection" was not been called.
This is my code, thx a lot!
//main.cpp
@#include <QApplication>
#include"QThread"
#include"QObject"
#include"QTextStream"
#include"kserver.h"
#include"testclass.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"main thread:"<<QThread::currentThreadId();QThread serverThread; KServer server(&a); testClass t; server.moveToThread(&serverThread); QObject::connect(&server.KLServer,SIGNAL(newConnection()),&server,SLOT(newConnect())); QObject::connect(&t,SIGNAL(t()),&server,SLOT(newConnect())); serverThread.start(); t.emits();//This is my test signal,this signal was work. QTextStream cin(stdin); QString iptCmd; while(1) { //qDebug()<<server.KLServer.isListening(); cin>>iptCmd; } return a.exec();
}
@//kerver.cpp
@#include "kserver.h"#include"QDebug"
KServer::KServer(QApplication *myApp, QObject *parent) :
QObject(parent)
{
myApp->processEvents();//I saw a post on StackOverflow, he said this will make it work, but it doesn't....
KLServer.listen(QHostAddress::Any,9825);
qDebug()<<"Server Thread"<<QThread::currentThreadId();
qDebug()<<"listening";}
void KServer::receiveMsg()
{
qDebug()<<"RM";
qDebug()<<QThread::currentThreadId();
}void KServer::newConnect()//never step into this function..
{
qDebug()<<"Server Thread"<<QThread::currentThreadId();
}
@"testClass" only have a signal "t()" and a function to emit the signal, other headers only use to declare.
Thanks a LOOOOOOT!!!!!!!
-
BTW, I'm sure the client connect to the server successfully, because if I close server side before client side, it will notice:The remote host closed the connection
-
You have this:
@ while(1)
{
//qDebug()<<server.KLServer.isListening();
cin>>iptCmd;
}
return a.exec();@This means your main application will never execute a.exec(), i.e. it will never enter the event loop
-
[quote author="gyll" date="1392737140"]You have this:
@ while(1)
{
//qDebug()<<server.KLServer.isListening();
cin>>iptCmd;
}
return a.exec();@This means your main application will never execute a.exec(), i.e. it will never enter the event loop[/quote]
Thanks a lot....I really make a stupid mistake.....
-
Just out of curiosity: what kind of app are you trying to build?
-
[quote author="gyll" date="1392737657"]Just out of curiosity: what kind of app are you trying to build?[/quote]
Eh..A game's server side..
-
Ok .. If your problem is solved.. then please tag it is as a [solved]