RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 814 | Replies: 2 | Thread Tools  Display Modes
Reply
Join Date: Nov 2008
Posts: 2
Reputation: Vux is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Vux Vux is offline Offline
Newbie Poster

How to download file from internet?

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2008
Posts: 2
Reputation: Vux is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Vux Vux is offline Offline
Newbie Poster

Re: How to download file from internet?

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

struct sockaddr_in webaddress;
int websock;

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, (struct sockaddr *) &webaddress, sizeof(webaddress)) < 0)
{
	perror("Failed to connect with the server \n");
	// TODO: printf or send msg back, or just create blank file
}
// Try to read from website, give error if not possible
//else if(( received = recv(websock,buffer,BUFFSIZE-1, 0)) < 0 )
else if(( received = read(websock,buffer,BUFFSIZE-1)) < 0 )
{
	perror("Failed to receive additional bytes from client \n");
}
// Read successful
else if(received > 0)
{
	printf("Received from Client : %s", buffer);
	memset(buffer, 0, sizeof(buffer));
}
// Close the client if badness occurred
else if(received < 1)
{
	printf("Closing this client (1)\n");
	close(websock);
	//pthread_exit(NULL);
}

// attempt download index.html
// send file back
// 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  
Join Date: Dec 2006
Location: india
Posts: 1,087
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 
Rep Power: 10
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: How to download file from internet?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:17 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC