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. QLocalServer and QLocalSocket for simple IPC
QtWS25 Last Chance

QLocalServer and QLocalSocket for simple IPC

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 615 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.
  • MecanikM Offline
    MecanikM Offline
    Mecanik
    wrote on last edited by
    #1

    Greetings, it's been a while since I've been here. Since then I've studied QT quite a lot and I can say that I'm extremely impressed with it.

    This subject/question has been asked on SO, but unfortunately that website has become quite... complicated in getting answers without someone closing your question, downvote it or more.

    With that being said I came here where the "magic" happens since all the QT experts are here :)


    I'm using the examples found at: https://doc.qt.io/qt-6/examples-ipc.html, with a small difference that I'm sending structures instead of a simple string.

    The code is essentially the same besides the QDataStream overloads as described here. I'm sending a small structure with an int and a QString.

    My current OS is Windows and it's unlikely it be executed on Unix anytime soon. Communication is working "ok" from server -> client, however I`m having a bit of setback in receiving from the client -> server.

    The confusion is coming from the example posted here. I`m not sure if I should replicate the same thing in the server receive function.

    At the moment on the server application on the QLocalSocket::readyRead slot I have:

    QLocalSocket* clientConnection = static_cast<QLocalSocket*>(sender());
    
    if (!clientConnection)
        return;
    
    QByteArray rcv_data = clientConnection->readAll();
    

    But again I repeat myself, not sure if this "correct". My questions are:

    • Why the blockSize statements in the first place?
    • If blockSize checking is really necessary, how to reset it after each message?
    • Should I copy the same checks with blockSize in the server application as well?

    Following the example I would like to have IPC functionality that allows sending/receiving both ways (the right way), without errors or disconnections 24/7.

    Any clarification/help/examples are much appreciated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should take a look at the chat example from the wiki. It uses transaction in order to handle data transmission. Granted it used JSON but it applies the same to your structure. You just need to implement the QDataStream operators for it.

      On a side note, it's qobject_cast that you shall use.

      static_cast does not do any type check so unless sender is already null, your if does not prevent any issue. You likely wanted to use dynamic_cast.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      MecanikM 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You should take a look at the chat example from the wiki. It uses transaction in order to handle data transmission. Granted it used JSON but it applies the same to your structure. You just need to implement the QDataStream operators for it.

        On a side note, it's qobject_cast that you shall use.

        static_cast does not do any type check so unless sender is already null, your if does not prevent any issue. You likely wanted to use dynamic_cast.

        MecanikM Offline
        MecanikM Offline
        Mecanik
        wrote on last edited by
        #3

        @SGaist Thanks, it doesn't really answer my questions but it does give me a solution. So in essence all that is different is an infinite loop inside readyRead(). A bit sceptic about it but I will do some testing. On that note, what do you mean with qobject_cast? I never heard/used it; quite new to QT.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can find the details in the QObject documentation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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