我正在使用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() << "Connected. Sending file to the server"; 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;
}
我之前開了一個帖子,在這裏:Sending data through socket spaces, receiving with spaces
人們不再幫助我。順便說一句,我不覺得我的問題完全得到了回答。所以我打開了另一個帖子。 我也看了常見問題解答部分,看看我應該怎麼做在這種情況下。但沒有成功。
無論如何,我現在的問題是:我應該刪除quint16嗎?我應該使用什麼來確定傳入數據包的大小呢?
謝謝,對於我可能犯的錯誤感到抱歉。
'用戶界面 - > QuestHtmlText-> toPlainText()' 不知道什麼'ui'在這裏做什麼,但你的接收代碼期望的Unicode,我會猜測,'toPlainText() 「是罪魁禍首,就像abarnet在第一個問題中所說的那樣,它在轉換過程中插入了一個無法識別的物料清單。 – John
常見問題確實會說[如果您對收到的答案不滿意,該怎麼辦](http://stackoverflow.com/faq#bounty)。 –
[通過套接字空間發送數據,使用空格接收](http://stackoverflow.com/questions/10890161/sending-data-through-socket-spaces-receiving-with-spaces) –