我需要通过QTcpSocket
发送二进制数据。我正在考虑使用QDataStream
,但我遇到了一个问题 - 如果在我尝试阅读时没有数据到达,它会无声地失败。
例如,如果我有这段代码:
QString str;
stream >> str;
如果套接字中当前没有数据,它将无提示失败。有没有办法告诉它阻止?
答案 0 :(得分:14)
问题有点严重。 Socket可以以块的形式接收数据,因此即使您等待waitForReadyRead
,它也会失败,因为没有足够的数据可以立即读取某个对象。
要解决此问题,您必须先发送一定数量的数据,然后再发送实际数据。
发送代码:
QByteArray block;
QDataStream sendStream(&block, QIODevice::ReadWrite);
sendStream << quint16(0) << str;
sendStream.device()->seek(0);
sendStream << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
在接收器上,您必须等到可用数据的大小满足要求。接收器代码或多或少看起来像这样:
void SomeClass::slotReadClient() { // slot connected to readyRead signal of QTcpSocket
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
QDataStream clientReadStream(tcpSocket);
while(true) {
if (!next_block_size) {
if (tcpSocket->bytesAvailable() < sizeof(quint16)) { // are size data available
break;
}
clientReadStream >> next_block_size;
}
if (tcpSocket->bytesAvailable() < next_block_size) {
break;
}
QString str;
clientReadStream >> str;
next_block_size = 0;
}
}
<小时/> 小更新,基于documentation,可以在不添加额外大小信息的情况下读取QString,因为传递给QDataStream的QString包含大小信息。尺寸可以像这样验证:
void SomeClass::slotReadClient() { // slot connected to readyRead signal of QTcpSocket
QTcpSocket *tcpSocket = (QTcpSocket*)sender();
while(true) {
if (tcpSocket->bytesAvailable() < 4) {
break;
}
char buffer[4]
quint32 peekedSize;
tcpSocket->peek(buffer, 4);
peekedSize = qFromBigEndian<quint32>(buffer); // default endian in QDataStream
if (peekedSize==0xffffffffu) // null string
peekedSize = 0;
peekedSize += 4;
if (tcpSocket->bytesAvailable() < peekedSize) {
break;
}
// here all required for QString data are available
QString str;
QDataStream(tcpSocket) >> str;
emit stringHasBeenRead(str);
}
}
答案 1 :(得分:10)
我从@ Marek的想法重新编写代码并创建了2个类--BlockReader和BlockWriter:
// Write block to the socket.
BlockWriter(socket).stream() << QDir("C:/Windows").entryList() << QString("Hello World!");
....
// Now read the block from the socket.
QStringList infoList;
QString s;
BlockReader(socket).stream() >> infoList >> s;
qDebug() << infoList << s;
class BlockReader
{
public:
BlockReader(QIODevice *io)
{
buffer.open(QIODevice::ReadWrite);
_stream.setVersion(QDataStream::Qt_4_8);
_stream.setDevice(&buffer);
quint64 blockSize;
// Read the size.
readMax(io, sizeof(blockSize));
buffer.seek(0);
_stream >> blockSize;
// Read the rest of the data.
readMax(io, blockSize);
buffer.seek(sizeof(blockSize));
}
QDataStream& stream()
{
return _stream;
}
private:
// Blocking reads data from socket until buffer size becomes exactly n. No
// additional data is read from the socket.
void readMax(QIODevice *io, int n)
{
while (buffer.size() < n) {
if (!io->bytesAvailable()) {
io->waitForReadyRead(30000);
}
buffer.write(io->read(n - buffer.size()));
}
}
QBuffer buffer;
QDataStream _stream;
};
class BlockWriter
{
public:
BlockWriter(QIODevice *io)
{
buffer.open(QIODevice::WriteOnly);
this->io = io;
_stream.setVersion(QDataStream::Qt_4_8);
_stream.setDevice(&buffer);
// Placeholder for the size. We will get the value
// at the end.
_stream << quint64(0);
}
~BlockWriter()
{
// Write the real size.
_stream.device()->seek(0);
_stream << (quint64) buffer.size();
// Flush to the device.
io->write(buffer.buffer());
}
QDataStream &stream()
{
return _stream;
}
private:
QBuffer buffer;
QDataStream _stream;
QIODevice *io;
};
答案 2 :(得分:3)
您可以调用QTCPSocket :: waitForReadyRead函数,该函数将阻塞直到数据可用,或连接到readyRead()信号,并在调用插槽时,从流中读取。