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 and QDataStream
Forum Update on Monday, May 27th 2025

QLocalSocket and QDataStream

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 991 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.
  • A Offline
    A Offline
    aliks-os
    wrote on last edited by aliks-os
    #1

    I am getting data from from QLocalSocket and try to put it to QDataStream. Then, I see data stream is empty, what I do wrong?

        auto socket = qobject_cast<QLocalSocket*>(sender());
        QByteArray bytes = socket->readAll();
        QDataStream stream(&bytes, QIODevice::ReadOnly);
    
        qDebug() << bytes;  // I got here
    
        while (!stream.atEnd())
        {
                QString receiveString;
                stream >> receiveString;
                qDebug() << receiveString;  // I not got here
        }
    
    jsulmJ 1 Reply Last reply
    0
    • A aliks-os

      I am getting data from from QLocalSocket and try to put it to QDataStream. Then, I see data stream is empty, what I do wrong?

          auto socket = qobject_cast<QLocalSocket*>(sender());
          QByteArray bytes = socket->readAll();
          QDataStream stream(&bytes, QIODevice::ReadOnly);
      
          qDebug() << bytes;  // I got here
      
          while (!stream.atEnd())
          {
                  QString receiveString;
                  stream >> receiveString;
                  qDebug() << receiveString;  // I not got here
          }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @aliks-os Do you mean the body of your while loop is never executed?
      Is bytes empty or does it contain something?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aliks-os
        wrote on last edited by
        #3

        qDebug() << receiveString;

        Does not contain anything. Loop executes

        J.HilkJ 1 Reply Last reply
        0
        • A aliks-os

          qDebug() << receiveString;

          Does not contain anything. Loop executes

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @aliks-os are you sure the bytes in your QByteArray are String conform?
          If not, then your QString may be terminated.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          A 1 Reply Last reply
          1
          • A Offline
            A Offline
            aliks-os
            wrote on last edited by
            #5

            yes I sure, I send a string from client

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @aliks-os are you sure the bytes in your QByteArray are String conform?
              If not, then your QString may be terminated.

              A Offline
              A Offline
              aliks-os
              wrote on last edited by
              #6

              @J.Hilk
              and I got this string here

              qDebug() << bytes; // I got here

              J.HilkJ 1 Reply Last reply
              0
              • A aliks-os

                @J.Hilk
                and I got this string here

                qDebug() << bytes; // I got here

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @aliks-os QByteArray is much more versatile than QString.

                Also do you send a QString from the client side? as QString stream operator is sure to come with an overhead that needs to be present in the data


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                A 2 Replies Last reply
                0
                • J.HilkJ J.Hilk

                  @aliks-os QByteArray is much more versatile than QString.

                  Also do you send a QString from the client side? as QString stream operator is sure to come with an overhead that needs to be present in the data

                  A Offline
                  A Offline
                  aliks-os
                  wrote on last edited by
                  #8

                  @J.Hilk Yes I send a QString from client side. Changed code as

                      while (!stream.atEnd())
                      {
                          qDebug() << "-1-";
                          QByteArray receiveString;
                          stream >> receiveString;
                          qDebug() << receiveString;  // I not got here
                      }
                  
                  
                  
                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @aliks-os QByteArray is much more versatile than QString.

                    Also do you send a QString from the client side? as QString stream operator is sure to come with an overhead that needs to be present in the data

                    A Offline
                    A Offline
                    aliks-os
                    wrote on last edited by
                    #9

                    @J.Hilk I got

                    "Hello12345"
                    -1-
                    ""

                    VRoninV 1 Reply Last reply
                    0
                    • A aliks-os

                      @J.Hilk I got

                      "Hello12345"
                      -1-
                      ""

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @aliks-os said in QLocalSocket and QDataStream:

                      I got
                      "Hello12345"

                      That means that bytes is just an array of (ascii?) encoded bytes. this is not how you use QDataStream normally.

                      Could you show us the code for the other side of the connection? the one sending the "Hello12345"?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      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