Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLocalSocket does not receive data
QtWS25 Last Chance

QLocalSocket does not receive data

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlocalsocketc++
4 Posts 3 Posters 835 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterBLB
    wrote on 22 Jun 2019, 08:44 last edited by
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 22 Jun 2019, 09:01 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MasterBLB
        wrote on 22 Jun 2019, 09:17 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 22 Jun 2019, 09:37 last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          1/4

          22 Jun 2019, 08:44

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved