开发者

Read and Send a file via QDataStream and QTcpSocket

My problem is that the variable content is always empty. Here is my code:

QFile file("/home/qt/Client/file.txt");
if(file.open(QIODevice::ReadOnly)){
    qDebug("File opened");
}

QDataStream fileData(&file);

QByteArray content;
QDataStream out(&content, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);  

out << static_cast<quint16>(0) << "file" << "/home/qt/Client/file.txt" << fileData;

out.device()->seek(0);
out << static_cast<开发者_运维知识库quint16>(content.size());

qDebug() << content.constData();

tcpSocket->write(content);

Output :

File opened

content is always empty

thanks for your help


content is not empty, but if you interpret it as a C-style 0-terminated string, it will appear to be empty.

When you write:

out.device()->seek(0);
out << static_cast<quint16>(content.size());

This will set the first two bytes of content to content.size() in big endian format (this is the default). So if content.size() is less than 255, the first byte of content.constData() will be 0 ('\0'). Any attempt to print constData() with a function that expects a C-style string will output nothing since your "string" starts with the "end-of-string" marker.

If you want to see the full contents of content, you should print all its chars separately and use something like hexdump to view the raw data.

Here's what I get if I do this instead of qDebug() << content.constData(); :

for (int i=0; i<content.size(); i++) {
    std::cout << content.constData()[i];
}

Output when run (the file contains just 20 'a' chars):

 $ ./qt | hexdump -C
00000000  00 40 00 00 00 05 66 69  6c 65 00 00 00 00 19 2f  |.@....file...../|
00000010  68 6f 6d 65 2f 71 74 2f  43 6c 69 65 6e 74 2f 66  |home/qt/Client/f|
00000020  69 6c 65 2e 74 78 74 00  00 00 00 14 61 61 61 61  |ile.txt.....aaaa|
00000030  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
00000040

If I had used:

std::cout << content.constData();

There would have been no output because of that very first 0 char.

If your data is longer, and the size of content is bigger than 255, the first char will no longer be 0, but you'll print two characters of garbage and nothing else because Qt serializes QString (and most other types) by first writing its length (32bit here), then its contents. Since its in big endian notation, the first byte has a very high chance of being 0.

Annotated output:

00000000  00 40 00 00 00 05 66 69  6c 65 00 00 00 00 19 2f  |.@....file...../|
          <u16> < str len > <  str data   > < str len > <
00000010  68 6f 6d 65 2f 71 74 2f  43 6c 69 65 6e 74 2f 66  |home/qt/Client/f|
                               str data...
00000020  69 6c 65 2e 74 78 74 00  00 00 00 14 61 61 61 61  |ile.txt.....aaaa|
            str data            >  <data len > < 
00000030  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
                    data                                 >
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜