I'm trying to make IPC work between one server to accept requests from multiple clients with Qt 4.6. All is working, except that the underlying named pipes are not being closed, making Qt to emit the maximum handle reached message (62).

Since I want to accept several client connections, I handle newConnections to the QLocalServer class as follows:

Qt Code:
  1. void ServerImpl::onNewConnection()
  2. {
  3. QLocalSocket * plsocket = _server->nextPendingConnection();
  4. SocketHandler* skh = new SocketHandler(plsocket, this, this);
  5. connect(plsocket, SIGNAL(readyRead()), skh, SLOT(socketReadyRead()));
  6. connect(plsocket, SIGNAL(error(QLocalSocket::LocalSocketError)), skh,
  7. SLOT(onError(QLocalSocket::LocalSocketError)));
  8. connect(plsocket, SIGNAL(disconnected()), skh, SLOT(disconnected()));
  9. }
To copy to clipboard, switch view to plain text mode 

Since every time I get a new connection the QLocalSocket pointer received from QLocalServer::nextPendingConnection() is overwritten, I manage the data requests through the SocketHandler class, which of course encapsulates a QLocalSocket class:

Qt Code:
  1. void SocketHandler::socketReadyRead()
  2. {
  3. QDataStream in(_ps);
  4. in.setVersion(QDataStream::Qt_4_5);
  5.  
  6. forever
  7. {
  8. if (_blocksize == 0)
  9. {
  10. if (_ps->bytesAvailable() < sizeof(blocksize_t))
  11. return;
  12.  
  13. in >> _blocksize;
  14. }
  15.  
  16. // We need more data?
  17. if (_ps->bytesAvailable() < _blocksize)
  18. return;
  19.  
  20. _srv->ReadRequest(in);
  21.  
  22. // CHECK: Discard remaining blocks
  23. _blocksize = 0;
  24. }
  25. }
To copy to clipboard, switch view to plain text mode 

Here's the class declaration:

Qt Code:
  1. class SocketHandler : public QObject
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. SocketHandler(QLocalSocket* ps, ServerImpl* srv, QObject* parent = 0) :
  7. QObject(parent), _ps(ps), _blocksize(0), _srv(srv) { };
  8.  
  9. virtual ~SocketHandler() {};
  10.  
  11. public slots:
  12. void socketReadyRead();
  13. void onError(QLocalSocket::LocalSocketError);
  14. void disconnected();
  15.  
  16. private:
  17.  
  18. QLocalSocket* _ps;
  19. blocksize_t _blocksize;
  20. ServerImpl* _srv;
  21. };
To copy to clipboard, switch view to plain text mode 

The problem, as stated above, is that I can't disconnect from server at client request. I tried QLocalSocket::close, QLocalSocket::disconnectFromServer without luck.

Should I go with a multithreaded solution? (I think it won't solve the problem).

Another solution I'm thinking is to offer a 1:1 QLocalServer<-->QLocalSocket IPC mechanism, instead of trying to handle 1:N.

Thanks in advance.