[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


  • Lifetime Qt Champion

    Hi,

    Might be a silly question but, did you check what QTcpSocket::error() returns ?



  • Yes, I checked the error message. It says "Network connection timed out". If I don't start the server, QTcpSocket returns the error string "Connection refused". So I guess, there is some kind of connection, but something is missing.



  • Hi,

    QTcpServer::incomingConnection() accepts a qintptr as parameter and it is defined as qint64 in a 64 bits platform.
    You're using intthat 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 offers Q_DECL_OVERRIDE to make your code compatible also for C++99



  • Thank you very much!
    That solved my problem.


Log in to reply
 

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