943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17068
  • C++ RSS
Nov 12th, 2008
0

How to download file from internet?

Expand Post »
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?
Similar Threads
Vux
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vux is offline Offline
2 posts
since Nov 2008
Nov 12th, 2008
0

Re: How to download file from internet?

Here's what I've got so far:

C++ Syntax (Toggle Plain Text)
  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?
Vux
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vux is offline Offline
2 posts
since Nov 2008
Nov 13th, 2008
1

Re: How to download file from internet?

> 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),
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Compiling in VC++ 2008 Express
Next Thread in C++ Forum Timeline: Template is killing me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC