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. [solved ] binary data to QTcpSocket

[solved ] binary data to QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 10.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Then, why not simply use QFile's "readAll()":http://qt-project.org/doc/qt-4.8/qiodevice.html#readAll ? It returns you a QByteArray with the data from the file

    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
    • S Offline
      S Offline
      S.ANAND
      wrote on last edited by
      #5

      Hi

      That is not working i am getting the same output the second image one.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #6

        Don't convert to ASCII. A char* is going to give you trouble the first time it encounters 0x00 (NULL) which will be interpreted as the end of the string. (JPEGs will certainly have a number of NULL characters in them -- I bet the first one you're encountering is at position 5 in your data.) A QByteArray will be sufficient to hold your data. You shouldn't be using a QTextStream at all, also. As SGaist said, QFile::readAll() should get you what you want. However, if you were insistent on using a stream, you'd need to use a QDataStream instead.

        How, exactly, are you checking your data?

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          S.ANAND
          wrote on last edited by
          #7

          Thats not working.

          I am checking like this
          @
          QFile file("d:/a.jpg");
          file.open(QIODevice::ReadOnly);
          ui->textEdit->append(file.readAll());
          file.close();
          @
          It is giving second image output.

          edit: please use @ tags around code sections; Andre

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #8

            Well of course it does. You need to realise that a JPEG file's content is not text and therefore any attempt to treat it is as such (e.g. with QTextStream or QTextEdit, notice the word Text) is doomed to fail. A JPEG binary file will typically have a zero in its fifth byte so you will generally get only four potential characters (byte FF D8 FF E0 in hex) before the string is considered ended. If byte 5 is not zero then the 11th byte will always be zero and you may see a recognisable "JFIF" in the text.

            Why don't you take a step back and explain the result you are expecting to achieve.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #9

              As ChrisW67 said, JPEG is NOT a text file so QTextEdit won't be able to support it. If you want to show it as text, at least convert it using QByteArray::toHex(). But I don't think we are on the right track then.

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                S.ANAND
                wrote on last edited by
                #10

                @ QFile file("d:/a.jpg");

                file.open(QIODevice::ReadOnly);
                

                this->sock = new QTcpSocket;

                connect(this->sock,SIGNAL(bytesWritten(qint64)),this,SLOT(q(qint64)));
                connect(sock,SIGNAL(connected()),this,SLOT(con()));
                connect(sock,SIGNAL(disconnected()),this,SLOT(dis()));
                
                this->sock->connectToHost("192.168.1.2",8080);
                
                this->sock->waitForConnected();
                
                sock->write(file.readAll().data());
                sock->waitForBytesWritten(-1);
                
                sock->close();
                

                file.close();
                @
                This is my code i need to send the image via network using QTcpSocket but the method QTcpSocket::write() will only support char * or bytearray but even if i convert the above image from QString to QByteArry and pass to QTcpSocket and send it is only showing 4 characters in the both cases QByteArray and char *.

                Edit: again, use @ tags around code sections please; Andre

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

                  Don't convert anything, just send the byte array created by readAll().

                  Besides, write(file.readAll().data()) is a bad idea. data() will return a pointer to the data from a temporary QByteArray created by file.readAll(). You don't really know when that byte array will be destroyed.

                  If you want to show the image on the other end, use a "QImage":http://qt-project.org/doc/qt-4.8/qimage.html + "QLabel":http://qt-project.org/doc/qt-4.8/qlabel.html combo

                  And please enclose your code with code tags so that it's cleaner to read.

                  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
                  • C Offline
                    C Offline
                    Code_ReaQtor
                    wrote on last edited by
                    #12

                    [quote author="S.ANAND" date="1363174874"] QTcpSocket::write() will only support char * or bytearray but even if i convert the above image from QString to QByteArry and pass to QTcpSocket and send it is only showing 4 characters in the both cases QByteArray and char *. [/quote]

                    It should work... The fact that you are using QTextEdit is NOT correct. QTextEdit only support "Texts".

                    Please visit my open-source projects at https://github.com/Code-ReaQtor.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      S.ANAND
                      wrote on last edited by
                      #13

                      Ha that's done the trick.
                      using file.readAll() instead of file.readAll().data()

                      Thank You SGaist and all others you replied. :)

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        S.ANAND
                        wrote on last edited by
                        #14

                        Yes that is also the mistake i made Code_ReaQtor Thank you .

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

                          You're welcome

                          If your issue is solved, please update the thread title and add [solved]

                          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