Solved

Download a file c++ with SOCKET

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

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
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
Comment Utility
Thank you, can you please explain what do you mean by not using a text mode fstream, what else can I use?
0
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
Comment Utility
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
Comment Utility
Great, thank you.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
You're welcome - I'm glad I could help ...

have a nice day,

best regards,

ZOPPO
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

12 Experts available now in Live!

Get 1:1 Help Now