C++ (Qt) while(!stream.atEnd()) { stream >> tmpData; }
C++ (Qt) sendStream << quint16(0) << dataToSend; // QString! sendStream.device()->seek(0); sendStream << (quint16)(messageForSend.size() - sizeof(quint16));
C++ (Qt)tcpConnection->write(messageForSend);
C++ (Qt)stream >> nextBlockSize;
C++ (Qt)stream >> tmpData; // QByteArray!
C++ (Qt)QByteArray slotDataSend( const QString & dataToSend ){ QByteArray messageForSend; QDataStream sendStream( &messageForSend, QIODevice::ReadWrite ); //sendStream.setVersion( QDataStream::Qt_DefaultCompiledVersion ); sendStream << quint16(0) << dataToSend; sendStream.device()->seek(0); sendStream << quint16( messageForSend.size() - sizeof( quint16 ) ); return messageForSend;} QString readResponse ( const QByteArray & array ){ static quint16 nextBlockSize = 0; QString tmpData; QDataStream stream( array ); //stream.setVersion( QDataStream::Qt_DefaultCompiledVersion ); while(true) { if ( nextBlockSize == 0 ) { if ( stream.device()->bytesAvailable() < sizeof(quint16) ) break; stream >> nextBlockSize; } if ( stream.device()->bytesAvailable() < nextBlockSize ) break; stream >> tmpData; nextBlockSize = 0; } return tmpData;} int main(int argc, char *argv[]){ QCoreApplication a( argc, argv ); QString text = QString::fromUtf8( "Привет, Русский язык!" ); QString result = readResponse( slotDataSend( text ) ); qDebug() << QString::fromUtf8( "Исходный текст:" ) << text; qDebug() << QString::fromUtf8( "Результат:" ) << result; return a.exec();}