| | |
How to download file from internet?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Here's what I've got so far:
It keeps saying:
Failed to connect with the server
: Bad file descriptor
Can anyone offer any help on my procedure above?
C++ Syntax (Toggle Plain Text)
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?
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> It keeps saying:
> Failed to connect with the server : Bad file descriptor
that says all that needs to be said, doesn't it?
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),
> 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)
#include <curl/curl.h> #include <cstdio> // link with libcurl. eg. // g++ -Wall -std=c++98 -pedantic -Werror -lcurl -I /usr/local/include -L /usr/local/lib void get_page( const char* url, const char* file_name ) { CURL* easyhandle = curl_easy_init() ; curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ; std::FILE* file = std::fopen( file_name, "w" ) ; curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file ) ; curl_easy_perform( easyhandle ); curl_easy_cleanup( easyhandle ); } int main() { get_page( "www.research.att.com/~bs/", "/tmp/stroustrup_home_page.html" ) ; }
![]() |
Similar Threads
- download any file!!! (Windows NT / 2000 / XP)
- Internet explorer/windows download error? (Windows NT / 2000 / XP)
- About;Blank Please Help, Hijack Log File (Viruses, Spyware and other Nasties)
- Howdo I download a .mim file in Macintosh (OS X)
- Problem with Internet exlorer downloads (Windows NT / 2000 / XP)
- Solution for slow internet browsing (Web Browsers)
- The Horse Power Of The Internet: The Newsgroups (Networking Hardware Configuration)
- Quicken on desktop but not in Applications-can't download data from internet (Mac Software)
- can't download on ie (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Compiling in VC++ 2008 Express
- Next Thread: Template is killing me
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






