QLocalSocket不接收数据

时间:2019-06-22 07:48:44

标签: c++ qt qlocalsocket

handleIPCLoad()中的套接字未接收任何数据。

我在构造函数中进行了检查,以查看已发送了多少个字节(如果有),并通过MasterBLB \ Mech Designs \ SDR-5V.mechDesign获得了字符串C:\ Mech Designer的更正结果60 在插槽中,我尝试了waitForRead(),但没有成功。

那我做错了吗?

//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);
}

0 个答案:

没有答案