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 2.1k 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 J.Hilk
    #5

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

    Please post your code...

    Client side Code:

     m_client1 = new QTcpSocket(this);
        m_client1->connectToHost("192.168.18.5", 10001);
        qDebug()<<"Connected. ";
    m_client1→write(" Module is ON ");
    QObject::connect(m_client1, &QTcpSocket::readyRead, this, &NetSndRcv::readData);
    readData();
    void NetSndRcv::readData(){
        
        QString str=m_client1->readAll();
        const char* v=str.toUtf8();
        //int str1= QString::fromUtf8(v);
        qDebug()<<"user:"<<v;
        ui->lcdNumber->display(v);
    
    }
    

    Server side code:

    void HwClientProc::SetSocket(int Descriptor)
    {
        //socket=NULL;
        socket = new QTcpSocket(this);
    
        connect(socket,SIGNAL(connected()),this,SLOT(connected()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    
        socket->setSocketDescriptor(Descriptor);
        //socket->write("previ_tmp");
    QProcess Temp_reading;
        int i;
        float Val_ave=0;
        float my_val=0;
        float iVal5=0;
    
           qDebug() << "previous_tmp  = " << previ_tmp;
    
    
             for(i=0;i<=20;i=i+1)
             {
    
                 Temp_reading.start("cat /sys/bus/iio/devices/iio:device0/in_voltage1_raw");
    
    
                 Temp_reading.waitForFinished(-1); // will wait forever until finished
                QString Temp_read = Temp_reading.readAllStandardOutput();
                 bool ok3;
    
                 iVal5 = Temp_read.toFloat(&ok3);
                 Val_ave=iVal5+Val_ave;
    
    
                 if (i==20)
                 my_val= Val_ave/20;
    
             }
                iVal5=(my_val)*0.610351562;
                iVal5=(iVal5-500)/10;
                iVal6=iVal5;
                if(iVal6 > previ_tmp)
                    current_temp=current_temp+1;
                else if(iVal6 < previ_tmp)
                    current_temp=current_temp-1;
    void HwClientProc::connected(){}
    
    void HwClientProc::disconnected(){}
    
    void HwClientProc::readyRead()
    {
        QString readsoc = socket->readAll();
    
        QString ipclient = socket->peerAddress().toString();
    
        QDate date = QDate::currentDate();
        QString datestr = date.toString("dd.MM.yyyy");
    
        QTime time = QTime::currentTime();
        QString timestr = time.toString("hh:mm:ss");
    
        qDebug() << '\t' << ipclient << "[" << datestr << "|" << timestr << "]:" << readsoc << '\n';
    
        //std::string readsocstd = readsoc.toStdString();
       // qDebug() << "\nData Recieved:" << readsoc;
        qDebug() << "\nHi Client:" << readsoc;
    
        
    {
    QByteArray data;
    
            data.resize(4);
            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);
    
        socket->write(data);
        socket->waitForBytesWritten(3000);
        socket->flush();
        qDebug() << "Temp value"<<data;
    }
    

    [@J.Hilk] added code tags to make it more readable

    jsulmJ 1 Reply Last reply
    0
    • MijazM Mijaz

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

      Please post your code...

      Client side Code:

       m_client1 = new QTcpSocket(this);
          m_client1->connectToHost("192.168.18.5", 10001);
          qDebug()<<"Connected. ";
      m_client1→write(" Module is ON ");
      QObject::connect(m_client1, &QTcpSocket::readyRead, this, &NetSndRcv::readData);
      readData();
      void NetSndRcv::readData(){
          
          QString str=m_client1->readAll();
          const char* v=str.toUtf8();
          //int str1= QString::fromUtf8(v);
          qDebug()<<"user:"<<v;
          ui->lcdNumber->display(v);
      
      }
      

      Server side code:

      void HwClientProc::SetSocket(int Descriptor)
      {
          //socket=NULL;
          socket = new QTcpSocket(this);
      
          connect(socket,SIGNAL(connected()),this,SLOT(connected()));
          connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
          connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
      
          socket->setSocketDescriptor(Descriptor);
          //socket->write("previ_tmp");
      QProcess Temp_reading;
          int i;
          float Val_ave=0;
          float my_val=0;
          float iVal5=0;
      
             qDebug() << "previous_tmp  = " << previ_tmp;
      
      
               for(i=0;i<=20;i=i+1)
               {
      
                   Temp_reading.start("cat /sys/bus/iio/devices/iio:device0/in_voltage1_raw");
      
      
                   Temp_reading.waitForFinished(-1); // will wait forever until finished
                  QString Temp_read = Temp_reading.readAllStandardOutput();
                   bool ok3;
      
                   iVal5 = Temp_read.toFloat(&ok3);
                   Val_ave=iVal5+Val_ave;
      
      
                   if (i==20)
                   my_val= Val_ave/20;
      
               }
                  iVal5=(my_val)*0.610351562;
                  iVal5=(iVal5-500)/10;
                  iVal6=iVal5;
                  if(iVal6 > previ_tmp)
                      current_temp=current_temp+1;
                  else if(iVal6 < previ_tmp)
                      current_temp=current_temp-1;
      void HwClientProc::connected(){}
      
      void HwClientProc::disconnected(){}
      
      void HwClientProc::readyRead()
      {
          QString readsoc = socket->readAll();
      
          QString ipclient = socket->peerAddress().toString();
      
          QDate date = QDate::currentDate();
          QString datestr = date.toString("dd.MM.yyyy");
      
          QTime time = QTime::currentTime();
          QString timestr = time.toString("hh:mm:ss");
      
          qDebug() << '\t' << ipclient << "[" << datestr << "|" << timestr << "]:" << readsoc << '\n';
      
          //std::string readsocstd = readsoc.toStdString();
         // qDebug() << "\nData Recieved:" << readsoc;
          qDebug() << "\nHi Client:" << readsoc;
      
          
      {
      QByteArray data;
      
              data.resize(4);
              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);
      
          socket->write(data);
          socket->waitForBytesWritten(3000);
          socket->flush();
          qDebug() << "Temp value"<<data;
      }
      

      [@J.Hilk] added code tags to make it more readable

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

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

      QString str=m_client1->readAll();

      If you send an integer as an integer (and not a string) this is not going to work.
      For proper conversion to int use https://doc.qt.io/qt-5/qbytearray.html#toInt and then convert the int to a string using https://doc.qt.io/qt-5/qstring.html#number-1

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

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

        @jsulm I implemented the following code in which getting value in str is "\u027"
        but in dec value is not updating and in lcdNumber value is displaying 0.

        QString str=m_client1->readAll();
        bool ok;
        int dec = str.toInt(&ok, 10);
        QString s = QString::number(dec,16);
        qDebug()<<"user:"<<s;
        ui->lcdNumber->display(s);

        jsulmJ 2 Replies Last reply
        0
        • MijazM Mijaz

          @jsulm I implemented the following code in which getting value in str is "\u027"
          but in dec value is not updating and in lcdNumber value is displaying 0.

          QString str=m_client1->readAll();
          bool ok;
          int dec = str.toInt(&ok, 10);
          QString s = QString::number(dec,16);
          qDebug()<<"user:"<<s;
          ui->lcdNumber->display(s);

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

          @Mijaz Did you actually read what I wrote?

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

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

            Kindly post me an example
            Thanks

            1 Reply Last reply
            0
            • MijazM Mijaz

              @jsulm I implemented the following code in which getting value in str is "\u027"
              but in dec value is not updating and in lcdNumber value is displaying 0.

              QString str=m_client1->readAll();
              bool ok;
              int dec = str.toInt(&ok, 10);
              QString s = QString::number(dec,16);
              qDebug()<<"user:"<<s;
              ui->lcdNumber->display(s);

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

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

              QString str=m_client1->readAll();

              This is wrong as I already wrote.

              bool ok;
              int i = m_client1->readAll().toInt(&ok, 10);
              qString numberStr = QString::number(i);
              

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

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

                I am sending string value from sever to the client which is ok, but I need to write from server to client an integer value which will be displayed on QlcdNumber on client-side. Which is displaying 0 there.

                jsulmJ 1 Reply Last reply
                0
                • MijazM Mijaz

                  I am sending string value from sever to the client which is ok, but I need to write from server to client an integer value which will be displayed on QlcdNumber on client-side. Which is displaying 0 there.

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

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

                  but I need to write from server to client an integer value

                  So, does your server send an int? What does ok contain after toInt()? Did you check what readAll() returns? Also, did you check how many bytes readAll() returns?

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

                  1 Reply Last reply
                  0
                  • 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

                                          • Login

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