Sending data over network does not work



  • 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." );
    }
    }
    @


  • Moderators

    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.



  • 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...



  • Try adding socket->flush() after the write


  • Moderators

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


Log in to reply
 

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