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



  • A connection to newConnection() signal is all you need, after listen.

    Can you post your code (not all, just the network part)?



  • 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_OBJECT

    public:
    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&#40;&#41;;
    

    }
    @



  • 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).



  • Btw ... use qDebug() function to show message in Application Output ...



  • all my printf worked fine but NewConnection...
    As an example, at the start of listenning : printf("Local Game Engine Server listenning on port %d...\n", port); => works fine in the console....



  • Well, your server starts fine (as netstat shows...)

    I just make a "telnet 127.0.0.1 8500" and a NEW CONNECTION!!! (my message) appears.

    Firewall? Do you connect to 127.0.0.1 and port 8500? I cannot see any other problem ...



  • I desactived the firewall but nothing changed.
    What is strang is that the client correctly onnects to the server and intercepts the connected() signal.
    Moreover, I need to put the WaitForNewConnection() to be able to intercept newConnection() signal....



  • telnet succeced in connecting to the server but still nothing in new connection!!!!



  • 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_OBJECT

    quint16 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_OBJECT

    public:
    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&#40;&#41;;
    

    }
    @

    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_LIB

    define GAMENETWORK_EXPORT Q_DECL_EXPORT

    #else

    define GAMENETWORK_EXPORT Q_DECL_IMPORT

    #endif)

    ==> May cause the issue?



  • I don't think so ... well, you can try :)



  • 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...!!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.