Solved

Download a file c++ with SOCKET

Posted on 2010-11-22
5
1,892 Views
Last Modified: 2012-06-27
Hi Guys,

I'm trying to download a file using a SOCKET in C++. I have a problem because my code returns an HTTP response instead downloading the file at that location.

    WSADATA WSAData; WSAStartup(MAKEWORD(2,0), &WSAData); 
    SOCKET sock; SOCKADDR_IN sin; 
    char buffer[1024];

        std::string srequete (GET http://www.test.com/test.exe HTTP/1.1\r\n");
	srequete += "Host: www.test.com\r\n";
	srequete += "Connection: close\r\n";
	srequete += "Keep-Alive: 115\r\n";
	srequete += "Referer: http://www.test.com/\r\n";
	srequete += "\r\n";c

        sock = socket(AF_INET, SOCK_STREAM, 0);
	sin.sin_addr.s_addr = inet_addr("xxx.yyy.www.zz"); 

	sin.sin_family = AF_INET;
	sin.sin_port = htons(80);

	connect(sock, (SOCKADDR *)&sin, sizeof(sin)); 
	send(sock, srequete.c_str(), srequete.length(), 0);
	
	i = recv(sock, buffer, sizeof(buffer), 0);  

	fstream filestr; 	
	filestr.open("Download/test.exe", fstream::in | fstream::out | fstream::app);	
	filestr << buffer << endl; 
        filestr.close();

        closesocket(sock); 
	WSACleanup();

Open in new window


My return and the content of my "test.exe" is this:

HTTP/1.0 200 OK

Date: Mon, 22 Nov 2010 12:05:28 GMT

Server: Apache

Content-Disposition: attachment; filename=test.exe

Content-Length: 5884016

Content-Type: application/msi

X-Cache: MISS from ip-xxx-yyy-www-zz.ip.test.net

X-Cache-Lookup: MISS from ip-xxx-yyy-www-zz.ip.test.net:80

Via: 1.0 ip-xxx-yyy-www-zz.ip.test.net (squid/3.1.7)

Connection: keep-alive

MZ

Open in new window


How can I get the return to be the content of the file instead a HTTP/1.0 200 OK so I can write it (download it) to my local file?

Is the "GET" in my request wrong? Should i use a different request?

It needs to use the SOCKET method in order for me to be able to use a proxy server to download this file.

Thank you
0
Comment
Question by:xNejX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 34187408
Hi xNejX,

in general I would say what you recieve is correct - since you send a HTTP request you recieve a HTTP response containing all this additional data.

The downloaded exe starts with the 'MZ' sequence, but IMO the rest of the data is truncated since by a zero-char. You need to find the beginning of the binary data from the HTTP response and save it to a binary file not using a text mode fstream.

Further the buffer you use is too small so you'll have to call the 'recv' in a loop until no more data is recieved; and within this loop you have to append recieved data to the above mentioned binary file.

Hope that helps,

ZOPPO
0
 

Author Comment

by:xNejX
ID: 34187550
Thank you, can you please explain what do you mean by not using a text mode fstream, what else can I use?
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
ID: 34187619
You can i.e. use a 'fstream' opened with mode flag 'ios::binary' set. Instead of using a streaming operator '<<' use 'write' instead.

Here you can find some helpful info about file in-/output in C++: http://www.cplusplus.com/doc/tutorial/files/
0
 

Author Closing Comment

by:xNejX
ID: 34187705
Great, thank you.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34187719
You're welcome - I'm glad I could help ...

have a nice day,

best regards,

ZOPPO
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question