Search code examples
qtqt4qlocalsocket

How to emit error for QLocalSocket


I am implementing 'Local fortune server' example (from Qt 4.7.3) as a service on Windows. What i want is that when someone paused the service, the local server should notify the error to the connected local socket (local fortune client). The error can be QLocalSocket::ServerNotFoundError. Now, How to generate this error from server example. Please look at the following code where i want to generate this error.

void FortuneServer::incomingConnection(quintptr socketDescriptor)
{
 if (disabled) {
  **// here i want to emit QLocalSocket::ServerNotFoundError**
  return;
 }

 QString fortune = fortunes.at(qrand() % fortunes.size());
 FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
 thread->start();
}

void FortuneServer:: pause()
{
  disabled = true;
}

Solution

  • If you want your server to notify your clients I think you should create and send your own message like :

    QString fortune = "Server down";
    

    But this will occure when your server has incoming message.
    or you can shutdown the server whith the QTcpServer::close() method

    void FortuneServer:: pause()
    {
        this.close();
    }
    

    your client app will lost its connection and you should be able to get the signal you want in it with this :

    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
                 this, SLOT(displayError(QAbstractSocket::SocketError)));