Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTcpSocket successfully connects, but not to my server. What is it connecting to?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTcpSocket successfully connects, but not to my server. What is it connecting to?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dusktreader
    wrote on last edited by
    #1

    I have successfully built a thin client/server using Qt's Tcp Sockets API. I know it works very well, because I have sent plenty of data over the wire and verified it. However, my project manager wants a suite of unit-tests, and I'm implementing them using Qt's Test Library.

    Anyhow, I'm trying to set up some dummy server to simply receive data from a QTcpSocket to verify a sendData() method in a unit test. When I connect the test socket, is shows that it is connected, but the slot for connecting the dummy server and its dummy socket is never called!

    Can anyone see what I'm doing wrong here? ( I've stripped the code down down to just the parts that seem broken in the test class )

    From tst_tcpcommsocket.h

    @#ifndef TST_TCPCOMMSOCKET_H
    #define TST_TCPCOMMSOCKET_H

    #include <QtCore/QString>
    #include <QtTest/QtTest>

    #include <iostream>

    #include <QTcpSocket>
    #include <QTcpServer>

    class TcpCommSocketTest : public QObject
    {
    Q_OBJECT

    private:
    QTcpSocket* qTestSocket;
    QTcpSocket* qDummySocket;
    QTcpServer* qDummyServer;

    public:
    TcpCommSocketTest();

    public slots:
    void connectDummyServer();

    private Q_SLOTS:
    void initTestCase();
    void sendDataTest();
    void cleanupTestCase();
    };

    #endif // TST_TCPCOMMSOCKET_H
    @

    From tst_tcpcommsocket.cpp
    @
    #include "tst_tcpcommsocket.h"

    void TcpCommSocketTest::connectDummyServer()
    {
    cout << "connection attempt" << endl;
    qDummySocket = qDummyServer->nextPendingConnection();
    }

    void TcpCommSocketTest::initTestCase()
    {
    qDummySocket = NULL;
    qDummyServer = new QTcpServer();
    qDummyServer->listen( QHostAddress("127.0.0.1"), 9000 );
    QVERIFIY( qDummyServer->isListening() );
    connect( qDummyServer, SIGNAL(newConnection()), SLOT(connectDummyServer()) );

    qTestSocket = new QTcpSocket();
    qTestSocket->connectToHost( QHostAddress("127.0.0.1"), 9000 );
    
    QVERIFY( qTestSocket->waitForConnected( 5000 ) );
    QVERIFY( qTestSocket->state() == QTcpSocket::ConnectedState );
    

    }

    void TcpCommSocketTest::sendDataTest()
    {
    int i=0;
    QVERIFY( qDummySocket != NULL );
    }

    QTEST_MAIN(TcpCommSocketTest);
    @

    The Test Run's output:
    @
    ********* Start testing of TcpCommSocketTest *********
    Config: Using QTest library 4.7.3, Qt 4.7.3
    PASS : TcpCommSocketTest::initTestCase()
    FAIL! : TcpCommSocketTest::sendDataTest() 'qDummySocket != NULL' returned FALSE. ()
    Loc: [-]
    cleanup
    PASS : TcpCommSocketTest::cleanupTestCase()
    Totals: 2 passed, 1 failed, 0 skipped
    @

    “Machines take me by surprise with great frequency.” -- Alan Mathison Turing

    1 Reply Last reply
    1
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      Did you try using a different port?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dusktreader
        wrote on last edited by
        #3

        Yes, I tried a few different ports. I tried ports in the 9000 - 9999 range. This is the range that my application uses, and it manages to connect and send data just fine.

        “Machines take me by surprise with great frequency.” -- Alan Mathison Turing

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dusktreader
          wrote on last edited by
          #4

          Someone from the StackOverflow forum answered my question very nicely:

          http://stackoverflow.com/questions/6433933/qtcpclient-successfully-connects-but-not-to-my-server-where-is-it-connecting

          bq. While QTEST_MAIN creates a QApplication and runs all your tests, it does so sequentially and not with an event loop. Therefore, while your socket can indeed connect to the server (meaning qTestSocket->waitForConnected() returns true), since the app doesn't return to an event loop, the QTcpServer's signal will not get emitted, and TcpCommSocketTest::connectDummyServer() never gets called.
          Try adding a call to qApp->processEvents() at the end of initTestCase(). It should let connectDummyServer() be called.

          It seems that I needed to add a qApp->processEvents() call in my initialization function.

          “Machines take me by surprise with great frequency.” -- Alan Mathison Turing

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved