Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not able to start qlocalserver

I use Qlocalsocket in my IPC solution and I am able to start the server and accept connections from QLocalSocket clients and communicate well, no prob in that.

but my problem happens when I kill the process of the server , I can't make QLocalServer listen on the same place again, I must change the service name to be able to start it again, which could not be possible at runtime environment.

so how to make the previous process to release that name?

here is how I start the server:

m_server = new QLocalServer(this);
if (!m_server->listen("serviceUniqueName")) {
    qDebug() << "Not able to start the Server";
    return;
}
like image 656
Dorgham Avatar asked Mar 26 '13 10:03

Dorgham


2 Answers

As Amartel pointed out, if the server dies, you need to delete the socket file. The best way to do is to call bool QLocalServer::removeServer ( const QString & name ):

m_server = new QLocalServer(this);
QString serverName("serviceUniqueName");
QLocalServer::removeServer(serverName);
if (!m_server->listen(serverName)) {
    qDebug() << "Not able to start the Server";
    return;
}

This way your call to listen will never fail.

like image 79
Alexey Avatar answered Nov 16 '22 09:11

Alexey


Qt help:

On Unix if the server crashes without closing listen will fail with AddressInUseError. To create a new server the file should be removed. On Windows two local servers can listen to the same pipe at the same time, but any connections will go to one of the server.

So, if you are using *nix, you should remove file "/tmp/socket_name".

like image 36
Amartel Avatar answered Nov 16 '22 09:11

Amartel