[SOLVED] Network Problems (QTcpSocket, QTcpServer)
-
Hello,
I have two applications, a server and a client. Both run under Windos 8.1 x64. The server uses a class called worker, which is derived from QTcpServer:private: QSet<QTcpSocket *> clients; Worker::Worker( QObject * parent ) : QTcpServer( parent ) { } Worker::~Worker( void ) { } void Worker::incomingConnection( int socket_descr ) { QTcpSocket * client = new QTcpSocket( this ); client->setSocketDescriptor( socket_descr ); clients.insert(client); connect( client, SIGNAL( readyRead() ), this, SLOT( readClient() ) ); connect( client, SIGNAL( disconnected() ), this, SLOT( discardClient() ) ); } void Worker::readClient( void ) { QTcpSocket * client = (QTcpSocket *)sender(); list<string> parameter; while( client->canReadLine() ) { string line_received( client->readLine().data() ); parameter.push_back( line_received ); } // do something with parameter and create result list<string>::iterator it; for( it = result.begin(); it != result.end(); ++it ) { client->write( QByteArray( it->c_str() ) ); } } void Worker::discardClient( void ) { QTcpSocket * client = (QTcpSocket *)sender(); clients.remove( client ); }
The Server starts listing by:
worker = new Worker( app ); if( !worker->listen( QHostAddress::Any, port ) || worker->hasError() ) { // An error occured return; }
The client uses an instance of QTcpSocket to connect to the server:
socket = new QTcpSocket( this ); socket->connectToHost( ip_address, ip_port ); if( socket->waitForConnected() ) { socket->write( QByteArray( ... ) ); } else { // error: probably connection not possible return; } if( !socket->waitForReadyRead( 60000 ) ) { // error: timeout waiting for response return; } list<string> result_buf; while( socket->canReadLine() ) { string line_received( socket->readLine().data() ); result_buf.push_back( line_received ); } socket->close();
The client connects and writes some data. After that, it is waiting for response but runs into a time out. The same code worked with an earlier version of Qt (5.0 or 5.2) running under Windows 7 32-bit, compiled with VS2010. Now it is compiled with VS2013 Community Edition for 64-bit. Firewall is switched off. I started the server in debug mode but nothing happens, incommingConnection() is not called.
What am I doing wrong?regards, Karl
-
Hi,
Might be a silly question but, did you check what QTcpSocket::error() returns ?
-
Hi,
QTcpServer::incomingConnection()
accepts aqintptr
as parameter and it is defined asqint64
in a 64 bits platform.
You're usingint
that is 32 bit; this also explain why your code works when compiler in 32bit.If you're using C++11 I suggest to use the
override
keyword to get informed when you're doing similar mistakes; Qt also offersQ_DECL_OVERRIDE
to make your code compatible also forC++99
-
I just wanted to tack a me-too on here. I've been converting a QTcpServer app over to 64-bit and it immediately stopped receiving signal. Connecting a 64-bit client to a 32-bit server still worked, so the problem had to be in the 64-bit server or one of the dependencies. As it turned out, the incomingConnection() had an int argument instead of a qintptr, a change we missed when upgrading to Qt5.