Reading array qt tcp socket
-
Hi everyone,
Here is my code,
I want to read more than one number of array but i can read only first number of array. Please help me!socket->waitForReadyRead(3000); QList<QByteArray> data = socket->readAll(); QDataStream something(data); something.setByteOrder(QDataStream::LittleEndian); / double result; something >>result; qDebug() <<result; socket->close();
-
Your code does not even compile. How do you send the data?
-
@sude said in Reading array qt tcp socket:
I want to read more than one number of array but i can read only first number of array
Then do
something >>result;
more than once as was already suggested in the other thread...
And also do not assume that readAll() will give you all you expect - when handling networking you need to accumulate data until you got everything. -
@Christian-Ehrlicher I send data with Codesys TCP code. It is my part of code, not all of them.
-
@sude said in Reading array qt tcp socket:
I want to read more than one number of array but i can read only first number of array. Please help me!
socket->waitForReadyRead(3000);
What is this code below? I don't believe this will ever compile!
QList<QByteArray> data = socket->readAll();
What is
socket
? It is a TCP or UDP socket?
If it is a TCP socket, are you sure you've got all data sent by remote?
As TCP is a stream interface, you could receive data partially.QDataStream something(data); something.setByteOrder(QDataStream::LittleEndian); / double result; something >>result; qDebug() <<result; socket->close();
-
From https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application:
The first thing to remember is that when we receive the
readyRead
we can't make any assumption on how much data is available, the signal only tells us some data is there hence there are 4 cases we need to handle:- The socket did not receive enough data to be a complete message, we only received a partial
- There is exactly enough data in the socket buffer to read a message
- There is more than enough data in the socket buffer to read a message but not enough to read 2 messages
- There is enough data in the socket buffer to read multiple messages