久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 QDataStream 從 QTcpSocket 讀取

Read from QTcpSocket using QDataStream(使用 QDataStream 從 QTcpSocket 讀取)
本文介紹了使用 QDataStream 從 QTcpSocket 讀取的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要通過 QTcpSocket 發(fā)送二進制數(shù)據(jù).我正在考慮使用 QDataStream,但我遇到了一個問題 - 如果在我嘗試讀取時沒有數(shù)據(jù)到達,它會默默地失敗.

I need to send binary data through a QTcpSocket. I was thinking about using QDataStream, but I've encountered a problem - it silently fails if no data has arrived at the time I try to read.

例如,如果我有這個代碼:

For example if I have this code:

QString str;
stream >> str;

如果套接字中當前沒有數(shù)據(jù),它將靜默失敗.有沒有辦法讓它改為阻止?

It will fail silently if no data is currently there in the socket. Is there a way to tell it to block instead?

推薦答案

我根據(jù)@Marek 的想法重新編寫了代碼并創(chuàng)建了 2 個類 - BlockReader 和 BlockWriter:

I reworked the code from @Marek's idea and created 2 classes - BlockReader and 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;

BlockReader:

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;
};

這篇關(guān)于使用 QDataStream 從 QTcpSocket 讀取的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产美女高潮 | 中文日韩在线 | 亚洲欧美一区二区三区国产精品 | 久久成人精品 | 精品永久| 欧美一区免费 | 老司机精品福利视频 | 中文字幕在线观看精品 | 欧美一区二区三区大片 | 国产在线精品一区二区 | 一区二区三区视频在线观看 | 国产成人在线免费 | 在线观看中文字幕 | 欧美日韩国产三级 | 嫩草视频网 | 日本成人免费网站 | 日韩成人在线观看 | 天天操天天摸天天爽 | 超碰97人人人人人蜜桃 | 亚洲精品永久免费 | 日韩欧美一区二区三区免费观看 | 日本久久一区 | 中文字幕电影在线观看 | 精国产品一区二区三区四季综 | 久久成人精品一区二区三区 | 色av一区二区三区 | 国产精品成人av | 久久久久久国产一区二区三区 | 久久亚洲精品国产精品紫薇 | 国产精品一区三区 | 一区二区伦理电影 | 精品久久久久一区二区国产 | 一区二区三区在线看 | 亚洲精品一区二区三区蜜桃久 | 日韩精品在线一区 | 黄色三级在线播放 | 欧美freesex黑人又粗又大 | 99精品国产一区二区三区 | 97成人在线 | 国产91久久久久久 | 国产婷婷色一区二区三区 |