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] Network Problems (QTcpSocket, QTcpServer)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Network Problems (QTcpSocket, QTcpServer)

Scheduled Pinned Locked Moved General and Desktop
networkqtcpsocketqtcpserver
6 Posts 4 Posters 8.1k Views 3 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.
  • K Offline
    K Offline
    karl_qt
    wrote on 29 May 2015, 13:12 last edited by karl_qt
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 May 2015, 21:39 last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        karl_qt
        wrote on 29 May 2015, 23:13 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 30 May 2015, 00:02 last edited by mcosta
          #4

          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

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          1
          • K Offline
            K Offline
            karl_qt
            wrote on 30 May 2015, 07:56 last edited by
            #5

            Thank you very much!
            That solved my problem.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sarielv
              wrote on 31 Oct 2018, 20:06 last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

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