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?

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?

> 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),

#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" ) ;
}
commented: So obvious really, if they stopped to think about it for a while. +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.