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. Reading array qt tcp socket
Forum Updated to NodeBB v4.3 + New Features

Reading array qt tcp socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 682 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.
  • S Offline
    S Offline
    sude
    wrote on last edited by
    #1

    Hi everyone,
    Here is my code,
    I want to read more than one number of array but i can read only first number of array. Please help me!

    socket->waitForReadyRead(3000);
    QList<QByteArray> data = socket->readAll();
    QDataStream something(data);
    something.setByteOrder(QDataStream::LittleEndian); /
    double result;
    something >>result; 
    qDebug() <<result;
    socket->close();
    
    jsulmJ KroMignonK 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your code does not even compile. How do you send the data?

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

      S 1 Reply Last reply
      1
      • S sude

        Hi everyone,
        Here is my code,
        I want to read more than one number of array but i can read only first number of array. Please help me!

        socket->waitForReadyRead(3000);
        QList<QByteArray> data = socket->readAll();
        QDataStream something(data);
        something.setByteOrder(QDataStream::LittleEndian); /
        double result;
        something >>result; 
        qDebug() <<result;
        socket->close();
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @sude said in Reading array qt tcp socket:

        I want to read more than one number of array but i can read only first number of array

        Then do

        something >>result;
        

        more than once as was already suggested in the other thread...
        And also do not assume that readAll() will give you all you expect - when handling networking you need to accumulate data until you got everything.

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

        S 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          Your code does not even compile. How do you send the data?

          S Offline
          S Offline
          sude
          wrote on last edited by
          #4

          @Christian-Ehrlicher I send data with Codesys TCP code. It is my part of code, not all of them.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @sude said in Reading array qt tcp socket:

            I want to read more than one number of array but i can read only first number of array

            Then do

            something >>result;
            

            more than once as was already suggested in the other thread...
            And also do not assume that readAll() will give you all you expect - when handling networking you need to accumulate data until you got everything.

            S Offline
            S Offline
            sude
            wrote on last edited by
            #5

            @jsulm something>>result; ---> fbef6620-1dd9-4ece-9af3-6d5a66425b34-image.png
            And my data is
            41ddf744-db87-4c2e-8f4b-59a872de7816-image.png

            1 Reply Last reply
            0
            • S sude

              Hi everyone,
              Here is my code,
              I want to read more than one number of array but i can read only first number of array. Please help me!

              socket->waitForReadyRead(3000);
              QList<QByteArray> data = socket->readAll();
              QDataStream something(data);
              something.setByteOrder(QDataStream::LittleEndian); /
              double result;
              something >>result; 
              qDebug() <<result;
              socket->close();
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @sude said in Reading array qt tcp socket:

              I want to read more than one number of array but i can read only first number of array. Please help me!

              socket->waitForReadyRead(3000);
              

              What is this code below? I don't believe this will ever compile!

              QList<QByteArray> data = socket->readAll();
              

              What is socket? It is a TCP or UDP socket?
              If it is a TCP socket, are you sure you've got all data sent by remote?
              As TCP is a stream interface, you could receive data partially.

              QDataStream something(data);
              something.setByteOrder(QDataStream::LittleEndian); /
              double result;
              something >>result; 
              qDebug() <<result;
              socket->close();
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              S 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @sude said in Reading array qt tcp socket:

                I want to read more than one number of array but i can read only first number of array. Please help me!

                socket->waitForReadyRead(3000);
                

                What is this code below? I don't believe this will ever compile!

                QList<QByteArray> data = socket->readAll();
                

                What is socket? It is a TCP or UDP socket?
                If it is a TCP socket, are you sure you've got all data sent by remote?
                As TCP is a stream interface, you could receive data partially.

                QDataStream something(data);
                something.setByteOrder(QDataStream::LittleEndian); /
                double result;
                something >>result; 
                qDebug() <<result;
                socket->close();
                
                S Offline
                S Offline
                sude
                wrote on last edited by
                #7

                @KroMignon Thank you for your response, and socket is tcp socket. I'm student and new here. Sometimes i make easy mistake, maybe there was another easy mistake and you can see it, i dont know. I cant solve the problem.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  From https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application:

                  The first thing to remember is that when we receive the readyRead we can't make any assumption on how much data is available, the signal only tells us some data is there hence there are 4 cases we need to handle:

                  1. The socket did not receive enough data to be a complete message, we only received a partial
                  2. There is exactly enough data in the socket buffer to read a message
                  3. There is more than enough data in the socket buffer to read a message but not enough to read 2 messages
                  4. There is enough data in the socket buffer to read multiple messages

                  "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
                  2

                  • Login

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