Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLocalSocket does not receive data



  • Dear Qt mates

    What I'm doing wrong so I don't receive any data in handleIPCLoad()?

    //QString Application::ipcKey = MechDesignerByMasterBLBServerKey;
    //QSharedMemory shared; <- in header file
    InstanceGuard::InstanceGuard(int argc, char **argv, MainWindow *w)
    :argc(argc), argv(argv), window(w), shared(Application::ipcKey)
    {
        if (shared.create(1))
        {
            QLocalServer::removeServer(Application::ipcKey);
            server = new QLocalServer();
            server->setSocketOptions(QLocalServer::UserAccessOption);
            server->listen(Application::ipcKey);
            connect(server, SIGNAL(newConnection()), this, SLOT(handleIPCLoad()));
    
            if (argc > 1)
            {
                QStringList files;
                files << QString::fromLocal8Bit(argv[1]);
                window->loadMech(files);
            }
        }
        else if (shared.error() == QSharedMemory::AlreadyExists && argc > 1)
        {
            QLocalSocket socket;
            socket.connectToServer(Application::ipcKey);
    
            // Wait for being connected
            if (socket.state() == QLocalSocket::ConnectingState )
            {
                socket.waitForConnected(250);
            }
    
            auto writtenBytes = socket.write(argv[1]);
            QMessageBox::information(0, "Written bytes", QString::number(writtenBytes));
            socket.flush();
            socket.waitForBytesWritten(250);
    
            exit(-4);
        }
    }
    
    void InstanceGuard::handleIPCLoad()
    {
        QLocalSocket *socket = server->nextPendingConnection();
        socket->waitForConnected(250);
        QStringList fileToLoad;
        fileToLoad.append(socket->readAll());
        socket->waitForReadyRead(250);
        QMessageBox::information(window, 0, fileToLoad.at(0));
        window->loadMech(fileToLoad);
    }
    

  • Lifetime Qt Champion

    Hi
    Are you spinning an event loop `?
    ( app.exec() )
    QLocalSocket can work without one, but you must use waitForNewConnection() then
    and not the
    connect(server, SIGNAL(newConnection()), this, SLOT(handleIPCLoad()));
    as newConnection() will never be sent then.



  • Starting QApplication::exec() is done later, after InstanceGuard object is created in main()
    As you can see there is a MessageBox in the handleIPCLoad(), which I see appears, just only without any data.


  • Qt Champions 2019

    Simply don't use the waitForFoo() functions to make sure you're running the eventloop. Use signals/slots instead.


Log in to reply