How do I Download a file ?

Hey how do i download a file from a web page and store it in some pre selected folder. Using coonsole Program.

I`am a noob so sorry if this is common knowlege.
Guesing i need to use sockets ?

just made this and now i am stuck not shure its correct ether.

Please someone shed some light on this for me.
And please do not refere me to MSDN i and google for ower a week.
I just need someone to realy dumb it down and explain whats happening.
If any one will do that i whode be for ever greatfull.

Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// sockets1.cpp : Defines the entry point for the Connectsole application.
//

#include "stdafx.h"
#include <iostream>
#include <winsock2.h>
// lib line here:
#pragma comment(lib, "Ws2_32.lib")


using namespace std;

int main(){ //Main Starts Here

	WSAData wsa;
	WORD Version = MAKEWORD(2, 1);
	WSAStartup (Version, &wsa); // Starup Function for Socket Using Adress to wsa.

	SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL); //Listening Port Type SOCK_STREAM also can be used SOCK_DGRAM
	SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);

	SOCKADDR_IN Server;

	Server.sin_addr.s_addr = inet_addr("http://google.com"); 
	Server.sin_family = AF_INET;
	Server.sin_port = htons(80); // Port goes HERE

	bind(Listen, (SOCKADDR*)&Server, sizeof(Server)); // Binds the listening port to Kisten SOCKET

	listen(Listen, SOMAXCONN); // How many Connection to listen for. (SOMAXCONN for Max Connections)
	int size = sizeof(Server);

	cout << "Listen" << endl;

	for(;;){
		if (Connect = accept(Listen, (SOCKADDR*)&Server, &size))
		{
			cout << "Funket!" << endl;
			 
			break;
		}
	}

	WSACleanup();
	cin.get();
	return 0;
}


Last edited on
You did ask this slightly more intelligently. But you still have some work to do.

No one is likely going to "dumb down" downloading a file like this. Unless you give good reason, I'll continue thinking that you are going to use this maliciously.
Here you go!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <iostream>


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create a HTTP client
    sf::Http Http;

    // Connect it to www.whatismyip.org
    Http.SetHost("www.whatismyip.org");

    // Prepare a request to retrieve the index page
    sf::Http::Request Request;
    Request.SetMethod(sf::Http::Request::Get);
    Request.SetURI("/");
    Request.SetBody("");
    Request.SetHttpVersion(1, 0);
    Request.SetField("From", "laurent.gom@gmail.com");

    // Send it and get the response returned by the server
    std::cout << "Sending a request to www.whatismyip.org..." << std::endl;
    sf::Http::Response Page = Http.SendRequest(Request);

    // Display the response
    std::cout << "Response received from www.whatismyip.org." << std::endl
              << "Status code (should be 200 on success): " << Page.GetStatus() << std::endl
              << "HTTP version: " << Page.GetMajorHttpVersion() << "." << Page.GetMinorHttpVersion() << std::endl
              << "Returned message (should be your IP address or an error message): " << Page.GetBody() << std::endl;

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}


That's how you do it in SFML!! Since you want to download a file, I think HTTP libraries are better than socket libraries.

Or you can try WinHttp http://msdn.microsoft.com/en-us/library/aa384081%28v=VS.85%29.aspx.
Just use Win32 api : 1 line of code (UDTF)
ultifinitus: If by maliciously. you mean some sort of "virus".
Its not its purly for educational purposes only.
Lerning C++ and gona use that to make a.
Game "Luncher" so you can download a "Luncher".
And in that consol app it will have a list of all my console games.

Like Tic Tac To, Number Guesing, Dungion "RPG" Game, and so one.
And when chosen one it will download the file for you and hopfully start it.
Then maybe when i get a litel bether. Make it delete the file ore say that its allready there and you dont have to download it again ore somthing but all in due time.
you mean some sort of "virus"

No. No I don't. I mean a malicious program.
For a straight one time download HTML is easier, cleaner and supported cross platform with basically no effort compared to C++. Seeing if your file is already there or not is part of the copy command for Win32, but why should that matter? You'll want to replace it with new versions everytime the user starts up anyway.
Topic archived. No new replies allowed.