Solved

Download a file c++ with SOCKET

Posted on 2010-11-22
5
1,780 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now