Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Sending data over network does not work

    General and Desktop
    3
    5
    1095
    Loading More Posts
    • 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
      karl_qt last edited by

      Hello,

      I have a windows service (running on Win 8.1) which acts as a server and I have a second programm as client. The client should send a command (string) to the service, the service executes the command and send back a result. The complete workflow should be as simple as possible: The client sends the command and waits for the result, no concurrency. The service processes all requests sequentially.

      At the moment, both programms run on windows 8.1 64 bit, I'm using Qt 5 and VisualStudio 2012
      , for the service I'm using QtService from Qt Solutions. Both, service and client are 32 bit programs. The Service runs fine, I can start and stop it on the windows system panel. But sending data does not work. It looks like the client sends data, but the service could not receive it. So the client waits forever for the response. The last service logmessage is "in readClient()" (Line 72, marked in the Sourcecode with ***), so I guess, a connection is established but no data could be read.

      What am I doing wrong?

      This is the code on client side:
      @int Client::sendBuffer( QByteArray write_buf, QString ip_address, unsigned int ip_port )
      {
      QByteArray read_buf;
      socket = new QTcpSocket( this );

      socket->connectToHost( ip_address, ip_port );
      if( socket->waitForConnected() )
      {
      socket->write( write_buf );
      }
      else
      {
      LOG_ERROR( "Verbindungsfehler: " << socket->errorString() );
      return 1;
      }

      do
      {
      if( !socket->waitForReadyRead( 300000 ) )
      {
      LOG_ERROR( "Netzwerkfehler: " << socket->errorString() );
      return 1;
      }

      read_buf.append( socket->readAll() );
      if( read_buf.endsWith( "###" ) )
      {
      LOG_DEBUG( "Gelesen: " << QString( read_buf ) );
      break;
      }
      }
      while( true );

      return 0;
      }
      @

      The network part of the service looks like this:
      @Worker::Worker( quint16 port, QDir start_dir, QObject * parent )
      : QObject( parent )
      {
      LOG_DEBUG( "Neuer Worker" );
      error = false;

      getSession();
      connect( my_server, SIGNAL( newConnection() ), this, SLOT( incomingConnection() ) );
      }

      void Worker::getSession( void )
      {
      LOG_DEBUG( "in getSession()" );
      QNetworkConfigurationManager manager;
      if( manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired )
      {
      // System default network configuration
      QNetworkConfiguration config = manager.defaultConfiguration();

      net_session = new QNetworkSession(config, this);
      connect( net_session, SIGNAL( opened() ), this, SLOT( sessionOpened() ) );

      LOG_DEBUG( "öffne Session (Signal)" );
      net_session->open();
      }
      else
      {
      LOG_DEBUG( "öffne Session (Aufruf)" );
      sessionOpened();
      }
      }

      void Worker::sessionOpened( void )
      {
      LOG_DEBUG( "in sessionOpened()" );
      QSettings settings( "C:\ProgramData\TestRun\TestRun.ini", QSettings::IniFormat, NULL );
      quint16 port = settings.value( "TestService/Port", 64738 ).toUInt();

      my_server = new QTcpServer( this );
      if( !my_server->listen( QHostAddress::Any, port ) )
      {
      LOG_ERROR( "Der TestService konnte nicht gestartet werden: " << my_server->errorString().toStdWString() );
      error = true;
      return;
      }
      }

      void Worker::incomingConnection( void )
      {
      if( disabled )
      {
      return;
      }
      LOG_DEBUG( "Neue Connection" );

      my_socket = my_server->nextPendingConnection();
      connect( my_socket, SIGNAL( readyRead() ), this, SLOT( readClient() ) );
      connect( my_socket, SIGNAL( disconnected() ), this, SLOT( discardClient() ) );

      QtServiceBase::instance()->logMessage("New Connection");
      }

      void Worker::readClient( void )
      {
      if( disabled )
      {
      return;
      }
      LOG_DEBUG( "in readClient()" ); // ***

      if( my_socket->canReadLine() )
      {
      QString input_buf( my_socket->readAll() );
      LOG_DEBUG( "Request: " << input_buf );

      QTextStream os( my_socket );
      os.setAutoDetectUnicode( true );
      os << "Response";
      my_socket->close();

      LOG_DEBUG( "An Client gesendet." );
      }
      }
      @

      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        Hi,

        @
        socket->write( write_buf );
        @

        When you send data from client, does it include "\n" at the end ?
        As canReadLine() will only return true when it receives a line i.e data ending with newline character.

        157

        1 Reply Last reply Reply Quote 0
        • K
          karl_qt last edited by

          I'm sorry, I put a "\n" at the end of the string, but it still doesn't work.

          If I stop the service, the client prints the error message in line 22. So it seems, the client sent the data and is waiting for the response.

          On the service-side, if I don't try to read data (line 74-77 removed), just sending back a response works. But the service needs an input to create a response...

          1 Reply Last reply Reply Quote 0
          • D
            DBoosalis last edited by

            Try adding socket->flush() after the write

            1 Reply Last reply Reply Quote 0
            • p3c0
              p3c0 Moderators last edited by

              Does it work if you remove canReadLine() and just do readAll() ?

              157

              1 Reply Last reply Reply Quote 0
              • First post
                Last post