Should I receive all messages after QLocalSocket is disconnected?

772 Views Asked by At

I have an application in c++/Qt in Windows, Linux and MacOSX, and I have a local client-server mechanism using Qt Local Server/Socket.

When the server refuses a connection, I send a message and close the socket, using QLocalSocket::disconnectFromServer()

From the documentation I see

If there is pending data waiting to be written, QLocalSocket will enter ClosingState and wait until all data has been written.

So in the client side, I connect the signal QLocalSocket::disconnected and do the following in my slot

void MyClientClass::onSocketDisconnected()
{
   qDebug() << "Socket disconnected";
   socket->readAll();
    //I do something with the data read
}

But it happens that sometimes, mostly on slow Linux machines (I try with virtual machines with only 1 processor), I do not receive the last message that the server send just before closing.

Why this happens? It should be in contrast with the documentation, shouldn't it?

Is there some motivation why I noticed it only in Linux and MacOSX (where Qt uses unix domain sockets) and I did not noticed it in Windows (where Qt uses pipes)?

Maybe there is some motivation related to domain sockets? Or I just misunderstood something?

Edit: as written in the comments below, in these cases when I do not receive the message, also the QLocalSocket::ClosingState is missing and I receive directly the QLocalSocket::UnconnectedState

Edit 2: as suggested in comments, I tried the waitForReadyRead() function. I did it onSocketDisconnected() and also onStateChanged() when the state goes to QLocalSocket::ClosingState or QLocalSocket::UnconnectedState. When I receive the QLocalSocket::ClosingState everything goes well, but the problem, as I said in the last edit, is that sometimes QLocalSocket::ClosingState is missing. In this case, the waitForReadyRead() fails without waiting the timeout and I have a warning "QIODevice:read (QTcpSocket): device not open" (even if I am using QLocalSocket, maybe the warning message is not updated well in Qt source). By the way, I am using Qt5.7.

0

There are 0 best solutions below