Important: Please read the Qt Code of Conduct -

Client PC is not sending TCP data. (solved)

  • 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.


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


  • Lifetime Qt Champion


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

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

  • Lifetime Qt Champion

    Do you also check the error signal ?

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

  • 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

  • Lifetime Qt Champion

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

  • Thanks, this is solved

    psocket = new QTcpSocket(this);
    if (psocket->waitForConnected(3000)) {
    qDebug()<<"Not Connected";


Log in to reply