Server-client receiving and sending problem using QtcpSocket for console application
-
@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.
-
@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. -
@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... -
@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;
-
@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.
-
@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)? -
@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!