html - 通过套接字空间发送数据,使用空间接收

标签 html c++ qt sockets large-data

为此,我将 C++ 与 QT4 结合使用。当我尝试发送大型 html 文件(在本例中为 8kb)时,发送和接收过程运行良好。但是收到的文件在 html 文件的每个字符之间带有空格。这里有一个例子, 文件是这样发送的:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">a</p></body></html>

然后收到了,像这样:

  ¼ < ! D O C T Y P E   H T M L   P U B L I C   " - / / W 3 C / / D T D   H T M L   4 . 0 / / E N "   " h t t p : / / w w w . w 3 . o r g / T R / R E C - h t m l 4 0 / s t r i c t . d t d " > 
 < h t m l > < h e a d > < m e t a   n a m e = " q r i c h t e x t "   c o n t e n t = " 1 "   / > < s t y l e   t y p e = " t e x t / c s s " > 
 p ,   l i   {   w h i t e - s p a c e :   p r e - w r a p ;   } 
 < / s t y l e > < / h e a d > < b o d y   s t y l e = "   f o n t - f a m i l y : ' M S   S h e l l   D l g   2 ' ;   f o n t - s i z e : 8 . 2 5 p t ;   f o n t - w e i g h t : 4 0 0 ;   f o n t - s t y l e : n o r m a l ; " > 
 < p   s t y l e = " - q t - p a r a g r a p h - t y p e : e m p t y ;   m a r g i n - t o p : 0 p x ;   m a r g i n - b o t t o m : 0 p x ;   m a r g i n - l e f t : 0 p x ;   m a r g i n - r i g h t : 0 p x ;   - q t - b l o c k - i n d e n t : 0 ;   t e x t - i n d e n t : 0 p x ; " > < / p > < / b o d y > < / h t m l >

我用于发送和接收的代码:

发送代码: qDebug() << "已连接。正在向服务器发送文件"; QString text = ui->QuestHtmlText->toPlainText();

if(text.length() < 1024)
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out << quint16(0) << QUESTION_HTML;
    out << text;
    out.device()->seek(0);
    out << quint16(block.size() - sizeof(quint16));
    qDebug() << "Block size: " << block.size();
    socket.write(block);
    return;
}

for(int i = 0; i < text.length(); i+=1024)
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out << quint16(0) << QUESTION_HTML;
    if((text.length() - i) > 1024)
        out << text.mid(i, i+1024);
    else
        out << text.right(1024 - i);
    out.device()->seek(0);
    out << quint16(block.size() - sizeof(quint16));
    qDebug() << "Block size: " << block.size();
    socket.write(block);
}

接收码:

qDebug() << "Writing File";
QDataStream in(this);
QString temp = "Teste.html", text;
QFile myFile(".//Questions//" + temp);
myFile.open(QIODevice::WriteOnly);
QDataStream out(&myFile);
while(!in.atEnd())
{
    in >> text;
    out << text;
}

最佳答案

@Eugen 我怀疑 quint16 要么根本没有写,要么在文件末尾。阅读下文。

@帕特里克don't repost只是因为你觉得服务不够快。 Stackowerflow 不是热线。

你在文件中得到的东西不是你的原始文本,而是 QString 序列化形式,这不出所料是 UTF-16。如果您想要返回文本,请将输入 QDataStream 读回 QString 并将其保存到文件中。

虽然给数据加上长度前缀通常是个好主意,但对于 QString >> QDataStream 来说绝对是多余的。读点东西herehere .此外,您已经开发出一种令人难以置信的混淆方式,我怀疑它什么也没做。 QByteArray 没有实现 QIODevice(事实上,为什么它应该)所以你的 out.device()->seek() 是一个基本的虚拟实现,empty and just returning true .如果在序列化转储文件的末尾找到您的长度“ header ”,我不会感到惊讶。

编辑:我认为您的 html 传输可能只有通过完全省略混淆的 quint 操作并使用 out QTextStream 而不是 QByteStream 才能开始正常工作。

关于html - 通过套接字空间发送数据,使用空间接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10890161/

相关文章:

jquery - 如何创建一个接受 24 小时制时间的 HTML 输入字段?

html - 我的网站在除 Firefox 之外的所有浏览器中都能正常显示

javascript - 选择文件后如何显示文件预览?

c++ - 严格弱序困惑

c++ - 通过 QTextStream 流式传输到 QTextEdit

c++ - 在 Qt 中将多个小部件合并为一个

html - 如何在 Html/MVC 中创建列?

c++ - "const method"读取成员变量时编译错误

php - C++ popen 运行 PHP

c++ - Qt - 使用内置翻译