No reception of NewConnection signal
-
Hi everyone,
As a training, i wrote a server/client application:
- A server exe, using my own class derivated from QTcpServer. Like examples, after listening on port, I connect signal newConnection with a slot (printing text to confirm a connection).
- A client exe, using my own class derivated from QTcpSocket. After the connection with server, I connect the signal connected() with my own slot( printint text to confirm connection to the server).
=> At execution, for cleint side, I have my printed text confirming the connection : connected() signal correctly treaten and redirected.
For server side, nothing printed and in a debug, I notice that my slot is not called : the signal newConnection is not interceped....Rk: It works once if only after the listenning function I call WaitForNewConnection() long enough for a client to connect on server before the end of wait time.....
Has someone an idea of the reason why I don't intercept the newConnection() signal?
Thanks a lot,
Cyte13
-
Thanks for your reply, it is what I think, but doesn' work...
Here is the threated version one:I have my own CGameEngine class derivated from QThread which has as a member a pointeur to another class CGameEngineServer which derivated from QTcpServer:
GAMEENGINE:
@
class GAMEENGINE_EXPORT CGameEngine : public QThread
{
Q_OBJECT
public:
/** Constructeur par défaut */
CGameEngine(QObject *parent = 0);/** Destructeur */ ~CGameEngine(); /** StartProcess : Lance le serveur de jeu local et gère la partie engineport : Port d'écoute du serveur local de jeu placeavailable : Nombre maximum de joueur à la table RETURN : GAMEENGINE_CORRECT ou GAMEENGINE_ERREUR **/ void StartProcess(quint16 engineport, int placeavailable); void run();
private:
//CComEngineBDD* MyComEngineBDD;
CGameEngineServer* MyEngineServer;
...void CGameEngine::StartProcess(quint16 engineport, int placeavailable)
{
m_engineport = engineport;
m_MaxPlayer = placeavailable;start();
}
void CGameEngine::run()
{
/* Create the server object /
MyEngineServer = new CGameEngineServer();
/ Start listenning on port in arg /
if (!MyEngineServer->Listen(m_engineport)) exit();
/ Connect signals between MyEngineServer and this to specify processes at a new incoming message */
QObject::connect(MyEngineServer, SIGNAL(MessageToRead(QString)), this, SLOT(MessageToRead(QString)));
exec();
}
@
GAMEENGINESERVER:
@
class GAMENETWORK_EXPORT CGameEngineServer : public QTcpServer
{
Q_OBJECTpublic:
CGameEngineServer(QObject *parent = 0);bool Listen(quint16 port); void Close();
protected:
//void incomingConnection(int socketDescriptor);private:
QVector<QTcpSocket*> m_VectorClient;
quint16 tailleMessage;signals:
void MessageToRead(QString);public slots:
void NewConnection();
void ReadMessage();
};bool CGameEngineServer::Listen(quint16 port)
{
bool result = listen(QHostAddress::LocalHost, port);
if (result)
{
printf("Local Game Engine Server listenning on port %d...\n", port);
/* make connection for a new connction */
QObject::connect(this, SIGNAL(newConnection()), this, SLOT(NewConnection()));
}
else
{
printf("Unable for Local Game Engine Server to listen on port %d...\n", port);
}
return result;
}@
and now the call to StartProcess :
@
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CGameEngine MyGameEngine;
MyGameEngine.StartProcess(8500, 2);return a.exec();
}
@ -
Well, I've tried your code in my PC ... and it works :)
Just a strange thing, but it's not dependent on your code. Qt Creator does not show printf messages in "Application Output". But if you launch your program in a console, you'll see the message (I put a printf in NewConnection slot).
-
-
well, the only thing left is that I can post my code and you'll check for any diff ...
@
#ifndef GAMESERVER_H
#define GAMESERVER_H#include <QtCore>
#include <QtNetwork>class CGameEngineServer;
class CGameEngine : public QThread
{
Q_OBJECTquint16 m_engineport;
public:
/** Constructeur par défaut */
CGameEngine(QObject *parent = 0) : QThread(parent), MyEngineServer(0) { }/** Destructeur */ ~CGameEngine() { } /** StartProcess : Lance le serveur de jeu local et gère la partie engineport : Port d'écoute du serveur local de jeu placeavailable : Nombre maximum de joueur à la table RETURN : GAMEENGINE_CORRECT ou GAMEENGINE_ERREUR **/ void StartProcess(quint16 engineport, int placeavailable); void run();
private:
CGameEngineServer* MyEngineServer;
};class CGameEngineServer : public QTcpServer
{
Q_OBJECTpublic:
CGameEngineServer(QObject *parent = 0) : QTcpServer(parent) { }bool Listen(quint16 port); void Close();
protected:
//void incomingConnection(int socketDescriptor);private:
QVector<QTcpSocket*> m_VectorClient;
quint16 tailleMessage;signals:
void MessageToRead(QString);public slots:
void NewConnection() { printf("NEW CONNECTION!!!\n"); }
void ReadMessage() { }
};#endif // GAMESERVER_H
@@
#include "gameserver.h"void CGameEngine::StartProcess(quint16 engineport, int placeavailable)
{
m_engineport = engineport;
// m_MaxPlayer = placeavailable;start();
}
void CGameEngine::run()
{
/* Create the server object /
MyEngineServer = new CGameEngineServer();
/ Start listenning on port in arg /
if (!MyEngineServer->Listen(m_engineport)) exit();
/ Connect signals between MyEngineServer and this to specify processes at a new incoming message */
QObject::connect(MyEngineServer, SIGNAL(MessageToRead(QString)), this, SLOT(MessageToRead(QString)));
exec();
}bool CGameEngineServer::Listen(quint16 port)
{
bool result = listen(QHostAddress::LocalHost, port);
if (result)
{
printf("Local Game Engine Server listenning on port %d...\n", port);
/* make connection for a new connction */
QObject::connect(this, SIGNAL(newConnection()), this, SLOT(NewConnection()));
}
else
{
printf("Unable for Local Game Engine Server to listen on port %d...\n", port);
}
return result;
}
@@
#include <QtCore/QCoreApplication>#include "gameserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CGameEngine MyGameEngine;
MyGameEngine.StartProcess(8500, 2);return a.exec();
}
@I hope that this will help you solve your problem ...
-
not better.....
The only difference between your code and mine is that the CGameEngine and CGameEngineServer are available via a library (ifdef GAMENETWORK_LIBdefine GAMENETWORK_EXPORT Q_DECL_EXPORT
#else
define GAMENETWORK_EXPORT Q_DECL_IMPORT
#endif)
==> May cause the issue?
-
I progress...
When removing all classes access by library and directly insert .h and .cpp files into my app folder, it workd fine!!!!Thanks a lot.
But... I would like to understand why it does not work when using StartProcess from the CGameEngine got from a library...!!!!