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. Sending data over network does not work
Forum Update on Monday, May 27th 2025

Sending data over network does not work

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.3k Views
  • 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 7 Dec 2014, 18:33 last edited by
    #1

    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
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 8 Dec 2014, 05:54 last edited by
      #2

      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
      0
      • K Offline
        K Offline
        karl_qt
        wrote on 8 Dec 2014, 21:29 last edited by
        #3

        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
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on 9 Dec 2014, 04:32 last edited by
          #4

          Try adding socket->flush() after the write

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 9 Dec 2014, 04:49 last edited by
            #5

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

            157

            1 Reply Last reply
            0

            1/5

            7 Dec 2014, 18:33

            • Login

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