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. Server-client receiving and sending problem using QtcpSocket for console application
Forum Updated to NodeBB v4.3 + New Features

Server-client receiving and sending problem using QtcpSocket for console application

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 4 Posters 1.8k 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.
  • MijazM Offline
    MijazM Offline
    Mijaz
    wrote on last edited by
    #13

    @jsulm I implement your above code its still giving me 0 value on QlcdNumber and in qDebug also.

    1 Reply Last reply
    0
    • MijazM Offline
      MijazM Offline
      Mijaz
      wrote on last edited by
      #14

      @jsulm from server-side I am not able to send the data in integer I am sending it therefrom QByteArray data; which is socket->write(data); and receiving it on client-side as "\u027" which is not displayed on QlcdNumber. I need the solution for this how I can display it on QlcdNumber.

      jsulmJ 1 Reply Last reply
      0
      • MijazM Mijaz

        @jsulm from server-side I am not able to send the data in integer I am sending it therefrom QByteArray data; which is socket->write(data); and receiving it on client-side as "\u027" which is not displayed on QlcdNumber. I need the solution for this how I can display it on QlcdNumber.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #15

        @Mijaz This is your code for sending int, right?

        data[0] = (uchar) (0x000000ff & current_temp);
        data[1] = (uchar) ((0x0000ff00 & current_temp) >> 8);
        data[2] = (uchar) ((0x00ff0000 & current_temp) >> 16);
        data[3] = (uchar) ((0xff000000 & current_temp) >> 24);
        

        Don't you think you need to do same on the receiver side?
        But actually it would be way easier to use https://doc.qt.io/qt-5/qdatastream.html on both sides.

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

        1 Reply Last reply
        3
        • MijazM Offline
          MijazM Offline
          Mijaz
          wrote on last edited by
          #16

          @jsulm I did QFile code on both side but I am getting an error QIODevice::read (QFile, "file.dat"): device not open on the receiver side. I am thinking on QTcpSocket how your QFile will work?

          jsulmJ KroMignonK 2 Replies Last reply
          0
          • MijazM Mijaz

            @jsulm I did QFile code on both side but I am getting an error QIODevice::read (QFile, "file.dat"): device not open on the receiver side. I am thinking on QTcpSocket how your QFile will work?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #17

            @Mijaz said in Server-client receiving and sending problem using QtcpSocket for console application:

            I am thinking on QTcpSocket how your QFile will

            Which QFile?!
            I never suggested to use any QFile!
            QDataStream also works with QByteArray as you can easily see from its documentation...

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

            1 Reply Last reply
            0
            • MijazM Mijaz

              @jsulm I did QFile code on both side but I am getting an error QIODevice::read (QFile, "file.dat"): device not open on the receiver side. I am thinking on QTcpSocket how your QFile will work?

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #18

              @Mijaz said in Server-client receiving and sending problem using QtcpSocket for console application:

              did QFile code on both side but I am getting an error QIODevice::read (QFile, "file.dat"): device not open on the receiver side. I am thinking on QTcpSocket how your QFile will work?

              You are going in wrong direction!
              Think simple!

              Qt has many helper classes.
              If I right understand your use case:

              • you have an external application which send an integer value per TCP.
              • first you said value is send as string, which means for me a text. Decimal value 1 is send as string "1".
              • then you said value is send as "raw data", which mean for me a binary. Decimal value 1 is send as 4 bytes ("\x00\x00\x00\x01" or "\x01\x00\x00\x00" depending on endiannes).

              To read as text value and convert it back to decimal:

              QString str=m_client1->readAll();
              bool ok;
              int value = str.toInt(&ok);
              if(!ok)
                qDebug() << "Could not read integer value";
              else
                qDebug() << "Recieved" << value;
              

              To read as binary value and convert it back to decimal:

              QByterArray b=m_client1->readAll();
              QDataStream stream(b);
              // to change byte order (endianess)
              // stream.setByteOrder(QDataStream::LittleEndian);
              int value;
              stream >> value;
              if(stream.status() != QDataStream::Ok)
                qDebug() << "Could not read integer value";
              else
                qDebug() << "Recieved" << value;
              

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

              1 Reply Last reply
              4
              • MijazM Offline
                MijazM Offline
                Mijaz
                wrote on last edited by
                #19

                @KroMignon Thanks for your excellent approach my value on QlcdNumber displayed successfully but not showing me the actual value current_temp from server-side it is displaying 385875968 instead of 23.

                JonBJ 1 Reply Last reply
                0
                • MijazM Mijaz

                  @KroMignon Thanks for your excellent approach my value on QlcdNumber displayed successfully but not showing me the actual value current_temp from server-side it is displaying 385875968 instead of 23.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #20

                  @Mijaz
                  How should anyone answer this? What is the server actually sending? Where in the stream does it appear? How are you reading it (you might check byte-by-byte initially that you get the right bytes)? How do you convert it to a number (e.g. is your bad number an endian-ness issue)?

                  jsulmJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Mijaz
                    How should anyone answer this? What is the server actually sending? Where in the stream does it appear? How are you reading it (you might check byte-by-byte initially that you get the right bytes)? How do you convert it to a number (e.g. is your bad number an endian-ness issue)?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @JonB This is what is sent:

                    data[0] = (uchar) (0x000000ff & current_temp);
                    data[1] = (uchar) ((0x0000ff00 & current_temp) >> 8);
                    data[2] = (uchar) ((0x00ff0000 & current_temp) >> 16);
                    data[3] = (uchar) ((0xff000000 & current_temp) >> 24);
                    

                    And as I already wrote before this needs to be considered on the receiver side, but did not hear any feedback...

                    @Mijaz If you use QDataStream on receiver side you also have to use it on the sender side!

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

                    MijazM JonBJ 2 Replies Last reply
                    2
                    • MijazM Offline
                      MijazM Offline
                      Mijaz
                      wrote on last edited by
                      #22

                      @JonB server is actually sending analog temperature sensor value in an integer which is continuously changing via QTcpSocket to client where it is displayed on QlcdNumber.

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @JonB This is what is sent:

                        data[0] = (uchar) (0x000000ff & current_temp);
                        data[1] = (uchar) ((0x0000ff00 & current_temp) >> 8);
                        data[2] = (uchar) ((0x00ff0000 & current_temp) >> 16);
                        data[3] = (uchar) ((0xff000000 & current_temp) >> 24);
                        

                        And as I already wrote before this needs to be considered on the receiver side, but did not hear any feedback...

                        @Mijaz If you use QDataStream on receiver side you also have to use it on the sender side!

                        MijazM Offline
                        MijazM Offline
                        Mijaz
                        wrote on last edited by
                        #23

                        @jsulm Let me try to use QDataStream on the sender side

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @JonB This is what is sent:

                          data[0] = (uchar) (0x000000ff & current_temp);
                          data[1] = (uchar) ((0x0000ff00 & current_temp) >> 8);
                          data[2] = (uchar) ((0x00ff0000 & current_temp) >> 16);
                          data[3] = (uchar) ((0xff000000 & current_temp) >> 24);
                          

                          And as I already wrote before this needs to be considered on the receiver side, but did not hear any feedback...

                          @Mijaz If you use QDataStream on receiver side you also have to use it on the sender side!

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #24

                          @jsulm
                          Ah I see! has anyone checked whether

                          it is displaying 385875968 instead of 23.

                          385875968 might be 23 in reverse bytes order (I don't know if it is)?

                          1 Reply Last reply
                          0
                          • MijazM Offline
                            MijazM Offline
                            Mijaz
                            wrote on last edited by
                            #25

                            @JonB thanks it's working now.

                            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