How to download file from internet?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: Vux is an unknown quantity at this point 
Solved Threads: 0
Vux Vux is offline Offline
Newbie Poster

How to download file from internet?

 
0
  #1
Nov 12th, 2008
Is there a way for example, to use WGET, inside of a C program?

I want to download the text of a website's index.html (ie www.google.com/index.html) and then send it over to another program via sockets.

Is this possible?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: Vux is an unknown quantity at this point 
Solved Threads: 0
Vux Vux is offline Offline
Newbie Poster

Re: How to download file from internet?

 
0
  #2
Nov 12th, 2008
Here's what I've got so far:

  1. struct sockaddr_in webaddress;
  2. int websock;
  3.  
  4. webaddress.sin_family = AF_INET;
  5. webaddress.sin_addr.s_addr = inet_addr("http://www.google.com/index.html");
  6. webaddress.sin_port = htons(80);
  7. if(connect(websock, (struct sockaddr *) &webaddress, sizeof(webaddress)) < 0)
  8. {
  9. perror("Failed to connect with the server \n");
  10. // TODO: printf or send msg back, or just create blank file
  11. }
  12. // Try to read from website, give error if not possible
  13. //else if(( received = recv(websock,buffer,BUFFSIZE-1, 0)) < 0 )
  14. else if(( received = read(websock,buffer,BUFFSIZE-1)) < 0 )
  15. {
  16. perror("Failed to receive additional bytes from client \n");
  17. }
  18. // Read successful
  19. else if(received > 0)
  20. {
  21. printf("Received from Client : %s", buffer);
  22. memset(buffer, 0, sizeof(buffer));
  23. }
  24. // Close the client if badness occurred
  25. else if(received < 1)
  26. {
  27. printf("Closing this client (1)\n");
  28. close(websock);
  29. //pthread_exit(NULL);
  30. }
  31.  
  32. // attempt download index.html
  33. // send file back
  34. // save file to local directory

It keeps saying:

Failed to connect with the server
: Bad file descriptor

Can anyone offer any help on my procedure above?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: How to download file from internet?

 
1
  #3
Nov 13th, 2008
> It keeps saying:
> Failed to connect with the server : Bad file descriptor

that says all that needs to be said, doesn't it?
int websock; // websock is an uninitialized int

webaddress.sin_family = AF_INET;
webaddress.sin_addr.s_addr = inet_addr("http://www.google.com/index.html");
webaddress.sin_port = htons(80);
if(connect(websock, ... // websock is not a file descriptor
              // it still is merely an uninitialized int

why don't you use a library? libcurl http://curl.haxx.se/libcurl/ is the one of choice.
and it's easy to use. eg. to fetch a page and save it to a file (without any error handling),
  1. #include <curl/curl.h>
  2. #include <cstdio>
  3.  
  4. // link with libcurl. eg.
  5. // g++ -Wall -std=c++98 -pedantic -Werror -lcurl -I /usr/local/include -L /usr/local/lib
  6.  
  7. void get_page( const char* url, const char* file_name )
  8. {
  9. CURL* easyhandle = curl_easy_init() ;
  10.  
  11. curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ;
  12.  
  13. std::FILE* file = std::fopen( file_name, "w" ) ;
  14. curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file ) ;
  15.  
  16. curl_easy_perform( easyhandle );
  17.  
  18. curl_easy_cleanup( easyhandle );
  19. }
  20.  
  21. int main()
  22. {
  23. get_page( "www.research.att.com/~bs/",
  24. "/tmp/stroustrup_home_page.html" ) ;
  25. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC