Solved

Download a file c++ with SOCKET

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

773 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