[Qt-interest] Sometimes reading data from QTcpServer I get the error "The remote host closed the connection"

Marc Cals cpmarc at gmail.com
Mon Apr 20 21:58:35 CEST 2009


Hello,

Using QTcpServer we have developed a module for a desktop application that
is listening on a network port for request of our clients. Our clients
connect to us and send us an Xml and we process it.

The protocol for send the XML is the client connect to server (in this case
our application) send an XML an disconnect without waiting any response of
the server.

Our code for waitingt client request is this (obviously this code is
executed in a QThread from our application)

    while (tcpRISServer.waitForNewConnection(-1)) //Waiting for new
connections
    {
        QTcpSocket *tcpSocket = tcpRISServer.nextPendingConnection();
        QString requestData;

        if (tcpSocket->waitForReadyRead(msTimeOutToReadData))
        {
            risRequestData= QString(tcpSocket->readAll());
        }
        else INFO_LOG("No data error: " + tcpSocket->errorString());

        tcpSocket->close();
        delete tcpSocket;
    }

The problem is that sometimes when we receive the request through QTcpServer
and try to read after waitForReadyRead() method of qTcpServer, we get the
error "The remote host closed the connection" and we can't read the data
that our client has send.

We think that the problem is that as our clients doesn't wait for any
response from us, they open the connection, send the xml (is and xml of 128
bytes) and close so fast that when our application try to get the data that
the client has send, using the readAll() method from QTcpServer, the socket
is closed and qt can't read the data getting this error "The remote host
closed the connection".

First of all if the reason why we get this error ? our conclusion is correct
? and if It is correct there is anyway of read the data from QTcpServer
independently the tcpSocket is close or not ?

The problem is that we can't change the protocol that our client use to send
us XML because it hasn't been developed for us.

We have reproduced this error with an small client that sends an XML
executed on the same machine that our application, the code of the client is
the next

    QTcpSocket tcpSocket;
    QString locaHostAddress = "127.0.0.1";

    tcpSocket.connectToHost(locaHostAddress, 18733);//Connectem contra el
localhost

    if (!tcpSocket.waitForConnected(1000)) //Esperem que ens haguem
connectat
    {
        errorConnecting(starviewerRisPort, tcpSocket.errorString());
        return;
    }

    tcpSocket.write(getXml().toLocal8Bit());
    tcpSocket.flush();

    tcpSocket.disconnectFromHost();//desconnectem
    tcpSocket.waitForDisconnected(5000);


Thanks for your help in advance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090420/3b54fb2a/attachment.html 


More information about the Qt-interest-old mailing list