QTcpSocket:read null data from socket
-
I'm using vm linux to develop qtcpsocket program, I can connect to the server device, and the server device continue send data, the wireshark can receive all the data correctly, and my qt program can receive the readyRead signal, but I can't read any thing from my slot function, the data I got always null. Some one please help me
following is my readyRead slot function's code
QByteArray cCanInfoSerials; QDataStream socketStream(m_pcSocket); socketStream.setVersion(QDataStream::Qt_5_13); // read to decide if the current board to be closed // and set display's backlight level // Data from Mainboard will be triggerred by DMSInfo module // start an infinite loop for (;;) { // we start a transaction so we can revert to the previous state in case we try to read more data than is available on the socket socketStream.startTransaction(); // we try to read the Can Data socketStream >> cCanInfoSerials; printf("Data Serail:"); for(int i=0;i<cCanInfoSerials.size();i++) { printf(" %02x ", cCanInfoSerials.at(i)); } printf("\n"); if (socketStream.commitTransaction()) { // we successfully read some data // we now need to make sure it's in fact a valid Can Data uint DataSize = cCanInfoSerials.size(); qDebug() << "CanDataSize:"<<DataSize; while(DataSize > sizeof(EthernetCanData)) { // read one DMSInfo and update DMSInfo Module EthernetCanData sCanInfoData; sCanInfoData.FF = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->FF; sCanInfoData.RTR = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->RTR; sCanInfoData.CanDataLength = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->CanDataLength; memcpy(sCanInfoData.DetailData, &reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->DetailData,sizeof(sCanInfoData.DetailData)); if(nullptr != m_pcCanDataManager) { m_pcCanDataManager->CanDataReceived(sCanInfoData); } // update size DataSize -= sizeof(EthernetCanData); } // loop and try to read more DMSInfo if they are available } else { // the read failed, the socket goes automatically back to the state it was in before the transaction started // we just exit the loop and wait for more data to become available qDebug() << "fail to commitTransaction"; break; } }
-
@lengjianhanshuang
I have do other test, and I found Qtcpsocket::size() returns 0, but Qtcpsocket::read() can actually read expected data, so in QtcpSocket, how should I know the real data size of socket? -
why is your read slot an infinite loop? readyRead is a signal that tells when data is available on a channel. The slot should read "available" data and return until readyRead signal is fired again.
-
@lengjianhanshuang and to add to @Kent-Dorfman: you are looking for bytesAvailable.
Regards
-
@kent-dorfman
Thanks for you replay, because every time I only need a small packet of the data to process, I don't know how many packets in the data buffer, and wether during my reading the buffer continue receiving new data, so I read them in the loop, until there is not enough data for me to read -
@lengjianhanshuang said in QTcpSocket:read null data from socket:
If I want to use committransaction
Why do you want to use it?
-
@aha_1980
from the doc, it seems the remaind data(not enough data bytes for processing) from previous package can be automatically contigous to data from next package, develop will do less process, so I 'd like to have a try... -
@lengjianhanshuang Indeed it can help you with that.
But you are reading
QByteArray
out of the data stream, how should the stream know where aQByteArray
begins or ends?That would only work if you actually send the data with
QDataStream
on the other side. -
@aha_1980
you are right, actually I don't know how to use QByteArray and QDataStream together correctly, the QDataStream has starttransaction and committransaction, my understanding is that, when QDataStream is used to manage the socket data, first use the start** to record the start point of Data stream in QDatastream, then try to "read some data" from QDataStream, at this point, we can use committransaction to decide whether it succeeded in "reading some data"(may be the size of "some data" is different from byteavailable), if it suceed, the "some data" in QDataStream will be clear, if it is not, the "some data" can be still in QDataStream using rolltransaction.
Is my understanding right?