c++ - Sending data through socket spaces, receiving with spaces -
i'm using c++ qt4 this. , when try send large html files(in case, 8kb), process of sending , receiving work well. file received come spaces between each character of html file. here example, file sent 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, this:
¼ < ! d o c t y p e h t m l p u b l 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 c t . d t d " > < h t m l > < h e d > < m e t n m e = " q r 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 { w h t e - s p c e : p r e - w r p ; } < / s t y l e > < / h e d > < b o d y s t y l e = " f o n t - f m l y : ' m s s h e l l d l g 2 ' ; f o n t - s z e : 8 . 2 5 p t ; f o n t - w e g h t : 4 0 0 ; f o n t - s t y l e : n o r m l ; " > < p s t y l e = " - q t - p r g r p h - t y p e : e m p t y ; m r g n - t o p : 0 p x ; m r g n - b o t t o m : 0 p x ; m r g n - l e f t : 0 p x ; m r g n - r g h t : 0 p x ; - q t - b l o c k - n d e n t : 0 ; t e x t - n d e n t : 0 p x ; " > < / p > < / b o d y > < / h t m l > the code i'm using sending , receiving:
sending code: qdebug() << "connected. sending file 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 = 0; < 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 bet there not spaces between characters (but 0s) & chars due use of quint16.
Comments
Post a Comment