Hi,

I'm working on an application which uses QLocalServer and QLocalSocket to communicate between processes which is not working quite as expected.

The client side is connecting like this:

Qt Code:
  1. socket.connectToServer("IPCCHANNEL");
To copy to clipboard, switch view to plain text mode 

In the slot connected to the connected() signal, we send the initial request data:

Qt Code:
  1. socket.write(reinterpret_cast<char*>(&m_nMessage), sizeof(int));
  2. socket.flush();
  3. socket.waitForReadyRead(20000);
To copy to clipboard, switch view to plain text mode 

In the slot connected to the readyRead() signal, we collect the returned information:

Qt Code:
  1. inputData.append(socket.readAll());
To copy to clipboard, switch view to plain text mode 

And finally, in the slot connected to the disconnected signal, we display the data and call qApp->exit(0).

From the server side, it works like this. In the slot attached to the incomingConnection() signal of the QLocalServer we get the new QLocalSocket connection and wait until it's ready to read the incoming request like this:

Qt Code:
  1. QLocalSocket *sock = localServer.nextPendingConnection();
  2.  
  3. if( !sock->waitForConnected() )
  4. return;
  5.  
  6. if( !sock->waitForReadyRead() )
  7. return;
  8.  
  9. QByteArray incoming(sock->readAll());
To copy to clipboard, switch view to plain text mode 

Then, once we have data to return, we send it back like this:

Qt Code:
  1. int length = response.length(); // response is a QByteArray
  2. sock->write(reinterpret_cast<char*>(&length), sizeof(int));
  3. sock->waitForBytesWritten(); // Send the return data length and wait for it to be written
  4.  
  5. int remaining = response.length();
  6. int written = 0;
  7. char *buf = response.data();
  8. do {
  9. written = sock->write(buf, remaining > 2048 ? 2048 : remaining);
  10. buf += written;
  11. remaining -= written;
  12. } while( remaining && written && sock->waitForBytesWritten(5000) );
  13.  
  14. sock->flush();
  15. sock->disconnectFromServer();
  16. sock->deleteLater();
To copy to clipboard, switch view to plain text mode 

But this only seems to work occasionally, the initial request always makes it to the server and the server always writes the information back to the socket but sometimes not all the data arrives and mostly the disconnected() slot is never hit. I'm obviously missing something here, can anyone shed any light on the problem?