1

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;
}
cyber_raj
  • 1,780
  • 1
  • 14
  • 25

2 Answers2

3

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)));
KaZ
  • 1,165
  • 7
  • 10
1

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.

Maciej
  • 3,685
  • 1
  • 19
  • 14
  • If i had to read about signals and slots y would i come here. Look at my code what exactly i want. – cyber_raj Aug 30 '11 at 12:23
  • Well, in Qt signals are emitted - so using word "emit" can cause misunderstanding. If you want to drop client connection, you can use abort() or close() to close socket. You should also thing about using QLocalServer to implement local server. – Maciej Aug 30 '11 at 12:54