Вопрос

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;
}
Это было полезно?

Решение

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)));

Другие советы

You can't emit QLocalSocket::ServerNotFoundError, because it's not a signal. You should define your own signal and emit it instead (you can pass values in signals). You should also implement slot and connect signal to it. Read more about signals and slots.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top