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. Client PC is not sending TCP data. (solved)

Client PC is not sending TCP data. (solved)

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 1.8k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    My server is not receiving data from my client.
    I used 2 object out (QDatastream) & block (QByteArray) to send TCP data.
    Below is my code
    Please highlight my mistake.

    @

                socket.connectToHost(ServerIP,10010);
                qint16 CRC;
                unsigned char TCPdata[5];
                TCPdata[0]=0x20;
                TCPdata[1]=DeviceNum.toInt();
                TCPdata[2]=0x00;
                CRC =ModbusCRC((unsigned char*)TCPdata,3);
    
    
    
                QByteArray block;
                QDataStream out(&block,QIODevice::WriteOnly);
                out.setVersion(QDataStream::Qt_5_0);
    
                out.device()->seek(0);               // serialize array input.
                out<<0x20<<DeviceNum.toInt()<<0x00<<CRC;  // i presumed this is sending data out via TCP
    
               socket.writeData(block.data(),block.size());
    

    @

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

      Hi,

      You don't check anywhere that the connection was successful, you should at least check that with waitForConnected or use the connected/error signals

      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
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

        I did use connected signals, but it never go into my slots.
        I am frustrated and insert all the slots code back into function.

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

          Do you also check the error signal ?

          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
          • H Offline
            H Offline
            houmingc
            wrote on last edited by
            #5

            can you explain why out and block?
            is there any link to sample code? i tried but i never really succeed sending the data

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

              Take a look at the size() at line 18. I guess it will not be the 5 bytes you are expecting but probably something like 14 bytes:
              0x20 => 4 byte integer
              DeviceNum.toInt() => 4 bytes
              0x00 => 4 bytes
              CRC => 2 bytes

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

                You're not answering the important question: are you checking whether you had an error when trying to connect to your server ?

                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
                • H Offline
                  H Offline
                  houmingc
                  wrote on last edited by
                  #8

                  Thanks, this is solved
                  @

                  psocket = new QTcpSocket(this);
                  psocket->connectToHost(ServerIP,10010);
                  if (psocket->waitForConnected(3000)) {
                  psocket->write(data);
                  psocket->waitForBytesWritten(1000);
                  psocket->disconnectFromHost();
                  }
                  else
                  {
                  qDebug()<<"Not Connected";
                  }

                  @

                  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