Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Rep Power: 0
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:
Rep Power: 0
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?
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 directoryIt keeps saying:
Failed to connect with the server
: Bad file descriptor
Can anyone offer any help on my procedure above?
•
•
Join Date: Dec 2006
Location: india
Posts: 1,087
Reputation:
Rep Power: 10
Solved Threads: 163
> 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
Other Threads in the C++ Forum
- download any file!!! (Windows x64 Edition)
- Internet explorer/windows download error? (Windows NT / 2000 / XP / 2003)
- 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 / 2003)
- 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
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode