QTcpClient successfully connects, but not to my server. Where does he connect?

I have successfully built a thin client / server using the Qt Tcp Sockets API. I know this works very well because I sent a lot of data on the wire and checked it. However, my project manager wants a set of unit tests, and I implement them using the Qt Test Library.

Anyway, I'm trying to set up some dummy server to just receive data from QTcpSocket in order to check the sendData () method in unit test. When I plug in a test socket, it shows that it is plugged in, 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 split the code down to 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 ); 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); 

Test run 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 
+4
source share
3 answers

While QTEST_MAIN creates QApplication and runs all your tests, it does it sequentially, not with an event loop. Therefore, if your socket can actually connect to the server ( qTestSocket->waitForConnected() returns true ), since the application does not return to the event loop, the QTcpServer signal QTcpServer not be emitted, and TcpCommSocketTest::connectDummyServer() will never be called.

Try adding a call to qApp->processEvents() at the end of initTestCase() . It should call connectDummyServer() .

+4
source

You can connect to the server instance that was launched earlier. Shake the process monitor to make sure you have no random cases. Does your test setup automatically start and shut down a dummy server? It may happen that the test successfully starts the server, but cannot kill it.

Also, out of 127.0.0.1, is the dummy server running on the same computer as the test code?

+1
source

Check the return value of qDummyServer->listen() . It should return true if the server is actually listening on port 9000. If it returns false , the likelihood that port 9000 is blocked by another process will not be able to listen to it. In this case, your qTestSocket connected to this other process instead of your own server.

+1
source

All Articles