Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

html - Receiving data with spaces, thru sockets

I'm using C++ with QT4 for this. And when I try to send large html files(in this case, 8kb), the process of sending and receiving work well. But the file received come with spaces between each character of the html file. Here an example, the file is sent like this:

<!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>

and it's received, like this:

  ? < ! 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 >

the code i'm using for sending and receiving:

Sending code:

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

Receiving code:

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

I did opened a post before, here: Sending data through socket spaces, receiving with spaces

People stopped helping me out. By the way, i didnt feel like my question was completly answered. So I opened another post. I also looked in the FAQ section to see what should I do in this case. But with no sucess.

Anyway, my question now is: should I remove the quint16? what should I use to determine the size of the incoming packet then ?

Thanks, and I'm sorry about the mistakes I may have made.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The stuff you are getting back in the file is not your original text, but QString serialization form, which is unsuprisingly UTF-16. If you want your text back, read the input QDataStream back to QString and save that into file.

While prefixing your data with a length is generally a good idea, it is absolutely redundant with QString >> QDataStream. Read up something here or here. Moreover you have developed a mind boggingly obfuscated way which i suspect is doing nothing. QByteArray is not implementing QIODevice (indeed, why it should) so your out.device()->seek() is a base virtual implementation, empty and just returning true. I won't be surprised if your length "header" is found at the end of your serialization dump file.

Edit: i think that your html transport might start working correctly only by leaving out the confused quint operation completely and use out QTextStream instead of QByteStream.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...