PDA

View Full Version : How to write contents of a QTcpSocket to a binary file?



Ali Reza
7th June 2012, 07:27
Hi all

i wrote a program that by HTTP protocol , connect to a download host and download a specific file.
for connection i used a QTcpSocket that connect to port 80 and send HTTP request,
when receive data from host, i don't know how to saves content of file not whole of received data to a binary file
please help me!

Regards.

yeye_olive
7th June 2012, 09:14
If I understand correctly, you want to know how to extract the contents of the file from the HTTP reply. This amounts to reimplementing (at least a part of) the HTTP protocol on top of QTcpSocket. Why don't you use the implementation (QNetworkAccessManager and related classes) already provided by Qt?

Ali Reza
8th June 2012, 09:09
bkz i love QTcpSocket
lol
:D

yeye_olive
8th June 2012, 10:37
With all due respect, this seems to me like a very wrong reason for reinventing the wheel. You would save yourself hundreds of lines of code and tens of hours of coding/debugging/maintenance by using an existing implementation, especially when it is well documented like Qt's. Anyway your question is not directly related to Qt, but to the HTTP.

wysota
8th June 2012, 21:35
i don't know how to saves content of file not whole of received data to a binary file

1. open file
2. if needed, preprocess the downloaded data according to the protocol your are using
3. write content of the downloaded data to the file
4. close file

To do #2, you need to:
1. learn about HTTP
2. learn about Base64 and GZIP/DEFLATE
3. learn about MIME

...or just change your affections and use QNetworkAccessManager.

Ali Reza
10th June 2012, 07:34
@ wysota
hello sir
please explain a little about "Base64 and GZIP/DEFLATE"

thanks in advance.

wysota
10th June 2012, 19:43
http://lmgtfy.com?q=base64
http://lmgtfy.com?q=deflate