How can I make c++ download something from internet to a folder? I've seen a dll made with c++ what downloads files but how can I do it?
Printable View
How can I make c++ download something from internet to a folder? I've seen a dll made with c++ what downloads files but how can I do it?
it depends on the operating system. "sockets" is a good search term to start with though.
There's lotsa ways, you're being incredibly vague. If you're on Windows and want very easy-to-use API's, you might check out the WinInet API with things like InternetReadFile or perhaps URLDownloadToFile. However, I think that connect()ing, send()ing a request and then recv()ing data from the socket would be a perfectly applicable choice for you. You can search the internet for something like a simple HTTP Get, and check out socket FAQ's, read Beej's socket tutorial, etc. I'm sure you'll find something to suit you.
Can you make me an example of it? Make me an example which dowloads a file to my computer. For example google logo.
Did you even try to look? An example of what? Okay well, Beej has graciously provided us with a stream socket example. It can be located here: http://www.ecst.csuchico.edu/~beej/g...l#simpleclient of course, this can be modified more than easily to fit winsock and the HTTP protocol. Simply start up winsock (WSAStartup()), and perform a send() of "GET /favicon.ico HTTP/1.1\r\nHost: meh.meh.meh\r\n\r\n" for example, and optionally, you could call inet_addr() before gethostbyname, as it is part of the Winsock Lame List. Again, this is only one example, and you never asked for which one to implement, and are again being vague. If you wanted to use InternetReadFile, look up InternetOpen, InternetOpenUrl, and then ReadFile API reference, it's almost identical in function. If you wanted URLDownloadToFile, well, it's incredibly simple, the callback is somewhat more difficult to implement though. A google search gets plenty of pretty nice results.
At least the MSDN function reference didn't explain anything... I dont even know the header file to be included...
So I need these headers: <netdb.h> <sys/types.h> <netinet/in.h> <sys/socket.h>
Where can i get them?
You can also use HTTP API
I looked there and that didnt explain anything
Maybe you should wait until you know more C++. You're probably wasting time just trying to skip to sockets, as you wont get the full benefit without knowing how to put it together in sensible/efficient ways.
The comments on the code on those sites is enough explanation needed when you know C++. Since you should know C++ before you move onto sockets it doesnt need to explain much, it just shows you which headers and which commands to use, then you look up the commands somewhere (MSDN, google, etc.) and put them together.
This is a simple tutorial on Winsocks, sort of like this sites C++ tutorials, so its easier to relate to: http://www.hal-pc.org/~johnnie2/winsock.html
>> You can also use HTTP API
Yeah I linked to that, it's called the WinInet API. It's kind of a dirty .......... but it's pretty cool I guess. I don't think you really gave a solid attempt to look for resources yourself, but if you want the utter most basic winsock tutorial in the world, MSDN has it, under the name "Getting Started with Winsock". The code is commented, all the functions ref's are linked and explained, and there is simply nothing more complex than a Hello World. Looky here: http://msdn.microsoft.com/library/de...th_winsock.asp
I had to chuckle at your request, maxorator, because what you're asking is a very tall request. It sounds like such a simple little favor, but you're asking for an "example" of something and that something happens to require someone to author the entire body of "your" program's primary functionality.Quote:
Originally Posted by maxorator
You are obviously very new to C++ programming. You really must learn and familiarize yourself with the language and it's concepts before you jump into something as complicated as socket programming. However, if you insist on persisting, do a lot of reading to earn a better comprehension before you jump into the icy waters. Good luck.
Quote:
Originally Posted by Tonto
that good link! thanks
I don't want to use it in any program, I just want to know how its done.Quote:
Originally Posted by LuckY
Ok, first errorQuote:
Originally Posted by Tonto
[Linker error] undefined reference to `WSAStartup@8'
Another error
4 C:\Programs\Dev-Cpp\Templates\main6.cpp `main' must return `int'
Quote:
Originally Posted by maxorator
main must start with int, and return a int,
the following is a example on how main should be declared.
for some applications you may needCode:int main()
{
return 0;
}
INT WINAPI MAIN()
or
int WINAPI MAIN()
either way it shoudl start with int and return int.
that will get rid of that error.
for your other error your not linking to something correctly
your prolyl doing a consoel application when you should
be picking a windows application on devc++.
The example there showed
Code:
#include <stdio.h>
#include "winsock2.h"
void main() {
return;
}
doesnt mean there right...
main always returns and int.
you dont have a a choice in the matter.
i noticed alot of code sample on msdn,
use void main(), but maybe its C related,
i dont know all the querks of C, but
for c++, since you are using a c++ compiler,
main returns int. no if's and's or but's
And this is their "complete" code:
Code:#include <stdio.h>
#include "winsock2.h"
void main() {
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return;
printf( "Bytes Recv: %ld\n", bytesRecv );
}
return;
}
If all you want to do is download files clean and simple, the solution was already given in the third post.
Check what headers and libs you need at the link given by Tonto. Make sure you check return values.Code:URLDownloadToFile( NULL, "http://www.google.com/intl/en_us/images/logo.gif", "c:\\google_logo.jpg", 0, NULL );
Maybe socket programming is a bit over your head just now and all you need is this simplified functionality.
changing it to int main(), and return 0;
will not affect your program in anyway, quit
acting like its life or death make the change and move on.
What header file do I need for that?Quote:
Originally Posted by nvoigt
:rolleyes:Quote:
Check what headers and libs you need at the link given by Tonto.
Ok, at last I found something that really works fine: http://www.whyaskwhy.org/programming....winh.cpp.html
I'm so glad you found a nice copy and paste example instead of reading the ample documentation we provided.
I cant use the ample documentation you provided because I dont have urlmon.h file! This is the only thing that works without urlmon.h.
out of curiosity how do you know you dont have it?
what compiler are you using?
Dev-c++
Which documentation required that file? There were about 4 listed and I'm sure it wasnt all of them, nor can I find the right one.. wasnt MSDN was it?Quote:
Originally Posted by maxorator
This thread is SOLVED!!! Someone's got problems understanding it
You would be one of them too maxorator, you replied "Dev-Cpp".Quote:
Originally Posted by maxorator
I'm sure its not a rule to not start 25 topics in 2 days asking questions you cant even handle the answer to, and still not say thanks once (in all these threads) when you even get information suiting your needs.
Well thats probably not a rule, and neither is replying trying to help you with the topic subject, especially since you only said that code segment worked, but wasnt your permanent choice.
I answered a question