QTcpServer::incomingConnection(qintptr handle) not firing?



  • Hi!
    I'm trying to create a multithreaded server using Qt for the first time. Normally one would use the socket pointer returned by the QTcpServer::nextPendingConnection() with the socket handle already baked in - but since I'm interfacing with the connecting client on a separate thread, I need to create the socket separately using the qintptr handle from QTcpServer::incomingConnection(qintptr handle). After a very dreary, error-packed debugging session I managed to track down the problem to the QTcpServer::incomingConnection() never being fired? These are the ones I've tried:

    • QTcpServer::incomingConnection(qintptr handle)
    • QTcpServer::incomingConnection(qintptr socketDescriptor)
    • QTcpServer::incomingConnection(int handle)

    Has anyone had a similar problem - has something changed over recent versions Qt?

    EDIT (The code):

    Creating instance of server:

    TestServer *myServer = new TestServer();
    myServer->initializeServer(1234);

    Which calls:

    void TestServer::initializeServer(quint16 port)
    {

    mainServer = new QTcpServer(this);

    mainServer->listen(QHostAddress::Any, port);

    qDebug() << "Listening for connections on port: " << port;

    }

    Server is now listening. When a client connects incomingConnection(qintptr handle) is supposed to be called:

    void TestServer::incomingConnection(qintptr socketDescriptor){

    TestClient *client = new TestClient(this);
    client->setSocket(socketDescriptor);

    }

    Which calls:

    void TestClient::setSocket(quint16 socketDescr)
    {
    socket = new QTcpSocket(this);

    socket->setSocketDescriptor(socketDescr);

    connect(socket, SIGNAL(connected()),this,SLOT(connected()));
    connect(socket, SIGNAL(disconnected()),this,SLOT(disconnected()));
    connect(socket, SIGNAL(readyRead()),this,SLOT(readyRead()));

    }

    Called on connect() signal:

    void TestClient::connected()
    {
    qDebug() << "Client connected..."; // This debug never appears in the console, since QTcpServer::incomingConnection isn't being fired.

    }


  • Moderators

    Are you aware of this example?
    Are messages displayed about connection problems when setSocket is called?
    You can also check the return value of connect, which will show you immediately that there is a problem. For instance

    bool boo = connect(socket, SIGNAL(connected()),this,SLOT(connected()));
    assert ( boo );
    

    Also according to documentation connected is emitted when connectToHost is called. AFAIK that is not part of your scheme.



  • Thank you for the swift response!

    Thanks, I was using the wrong signal - just fixed it. I do know, however, that incomingConnection() is not being called - since the breakpoint is never reached; so the problem persists. No error messages from setSocket.

    On another topic - in the Fortune example, they're subclassing QThread: isn't that the wrong way of doing it? Or has it become acceptable again after some recent Qt version?


  • Moderators

    Where do you connect the signal of incomingConnection?


Log in to reply
 

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