QTcpServer Issue



  • Hi all,

    Am experiencing problems with QTcpServer where incomingConnection is not firing.(Although newTCPConnection is!)

    My class definition is as follows:

    @
    #ifndef TCPSERVER_H
    #define TCPSERVER_H

    #include <QTcpServer>

    class tcpserver : public QTcpServer
    {
    Q_OBJECT

    public:
    tcpserver(QObject *parent = 0);

    protected:
    void incomingConnection(int socketDescriptor);

    public slots:

    signals:

    private:
    };

    #endif // TCPSERVER_H
    @

    The header as follows:

    @
    include "tcpserver.h"

    //
    // TCP Server constructor
    //

    tcpserver::tcpserver(QObject *parent)
    : QTcpServer(parent)
    {
    }

    //
    // Override incomingConnection to allow TCP Socket to be processed by threads
    //

    void tcpserver::incomingConnection(int socketDescriptor)
    {

    qDebug() << "Got a incoming connection";
    

    }
    @

    However, when I use the class:

    In my calling class header
    @
    ...
    tcpserver *m_tcpserver;
    ...
    @

    In my calling class implementation:

    @
    ...
    m_tcpserver = new tcpserver(this);
    ...
    connect(m_tcpserver, SIGNAL(newConnection()), this, SLOT(newTCPConnection()));
    ...
    if (!m_tcpserver->listen(QHostAddress::Any, m_tcp_port))
    {
    emit debugMessages(QString("REMOTE: Unable to start TCP Server."));
    }
    else
    {
    emit debugMessages(QString("REMOTE: TCP Server started successfully."));
    }
    ...

    void caremote::newTCPConnection()
    {
    emit debugMessages("TCP SERVER: Got a new connection");
    }
    @

    This compiles and runs with no errors however when a client connects, incomingConnection is never fired!

    What am I doing wrong?


  • Moderators

    Hi

    Does it work if you move "listen(QHostAddress::Any, m_tcp_port)" to the tcpserver's constructor ?



  • Same symptoms .... I have tried on Ubuntu, OSX and Windows with Qt5.0.1.
    I thought it might be a bug and tried Qt4.8.3 but same again !

    Weird - I'm sure I've used this before with success.


  • Moderators

    It works for me. i'm using Ubuntu and Qt 4.8.3. First incomingConnection() is called and then the newConnection() signal is emitted and hence the respective slot is called.
    Make sure that any firewall is not blocking the conenction.



  • The client confirms the connection is made and some data sent ! The newConnection() is firing, hence the connection is being registered at the QTcpServer, however, incomingConnection() is not being called.

    Are there any situations where an override is not called? I believe I have the signature correct:

    @protected:
    void incomingConnection(int socketDescriptor);
    @

    As it comes up in itallics and I have the implementation .... I can't figure why it is not being called.



  • BTW, PRO file as follows:

    @
    QT += core gui network

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = Remote
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp
    remote/remote.cpp
    remote/networkmessages.cpp
    tcpthread/tcpthread.cpp
    tcpserver/tcpserver.cpp

    HEADERS += mainwindow.h
    remote/remote.h
    remote/networkmessages.h
    tcpthread/tcpthread.h
    tcpserver/tcpserver.h

    FORMS += mainwindow.ui
    @



  • Anybody else have ideas?



  • Maybe a bit late, but just in case you are interested. I had the same problem and figured it out.

    The reason for incomingConnection is not being called is likely to be caused by wrong type of parameter for the handle. A methods signature consists both of the method name AND arguments list, thus if you want to overwrite a virtual method you have to take care that both match perfectly.

    The signature for incomingConnection has changed from:
    incomingConnection(int) .. to ..
    incomingConnection(qintptr)

    QtCreator will falsely highlight your methods name, even if you use an int instead of qintptr. Changing from int to qintptr solved the problem for me.


Log in to reply
 

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