Tuesday, March 26, 2013

Not able to start local socket server

Problem:
if you try to start local socket server on unix using QLocalServer for IPC and the server crashed without closing , then when you try to start it again, listen will fail unless you change the serverName you are listening on...

here is a sample code:

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

proposed solution:

when the local socket server starts, it creates a file in /tmp with the name of the server, so next attempt to listen will find this address in use, you should remove the file /tmp/serverName to be able to start again.

sample code:
 
m_server = new QLocalServer(this);
    if (!m_server->listen("serverUniqueName")) {
        if(!QFile::remove("/tmp/serverUniqueName") ||
                !m_server->listen("serverUniqueName")){
            qDebug() << "Not able to start the Server";
            return;
        }
    }