Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Download a file c++ with SOCKET

Posted on 2010-11-22
5
Medium Priority
?
2,172 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 2000 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Six Sigma Control Plans

688 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