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.@
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());
@
-
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
-
Do you also check the error signal ?
-
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);
psocket->connectToHost(ServerIP,10010);
if (psocket->waitForConnected(3000)) {
psocket->write(data);
psocket->waitForBytesWritten(1000);
psocket->disconnectFromHost();
}
else
{
qDebug()<<"Not Connected";
}@