PDA

View Full Version : Local client socket not emitting error when server not running



nitks.abhinav
27th October 2017, 18:47
Hi,

I have a local client socket and it works fine i.e. connects to local (Unix domain) server , receives data.

But when server is not running it does not emit the error signal, below is the code.

Thanks,



void ClientThread::serverConnect()
{
//Connect to the local notification srever
socket = new QLocalSocket(this);
qDebug() << "Client thread: connecting to notification server\n";
socket->connectToServer("/usr/bin/fortune");
qDebug() << "Client thread: connected to server";

connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()), Qt::DirectConnection);
connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(displayError(QLocalSocket::LocalSocketError)) );
}

void ClientThread::displayError(QLocalSocket::LocalSock etError socketError)
{

switch (socketError) {
case QLocalSocket::ServerNotFoundError:
qDebug() << "Client thread: The host was not found. Please check the "
"host name and port settings.";

break;
case QLocalSocket::ConnectionRefusedError:
qDebug() << "Client thread: The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct.";
break;
case QLocalSocket::PeerClosedError:
qDebug() << "Client thread: peer closed error.";
break;
default:
qDebug() << "Client thread: The following error occurred: " << socket->errorString();
}
}


Added after 1 37 minutes:

Got it resolved, had to put " socket->connectToServer("/usr/bin/fortune");" before connecting signal/slot.


Thanks

d_stranz
27th October 2017, 21:00
Got it resolved, had to put " socket->connectToServer("/usr/bin/fortune");" before connecting signal/slot.

You mean "after connecting signal / slot", don't you? If you try to connect to the server and it fails, the local socket will emit the error() signal, but unless you have connected your QLocalSocket instance's error() signal to a slot, you won't see the signal.