Hi there!

Of *course* this is "cost effective" in C!! You just have to do a tiny
bit of sockets... C should be faster than an interpreted language,
although most of the delay for this sort of thing is probably due to the
speed of the network...

Attached is a rather simple example that I wrote real quickly... Seems to
get the job done though, just write it to stdout.

I found two other lightweight C programs on http://www.linuxapps.com (a
groovy Linux site)

They are:

* Swebget at http://www.uni-hildesheim.de/~smol0075/webget/

* Curl at http://www.fts.frontec.se/~dast/curl/

Curl is the biggest...

There are other programs like this under "Tools" under the "Web" section
on linuxapps.com; GNU wget, written in C as well, is there and does
recursive mirroring...

Hope this helps! :)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>

#define HOST "sunsite.unc.edu"
#define PATH "/pub/"  /* Notice the leading slash, 'tis required */

void die(char *s);

main()
{
 char buf[512];
 int n = 1;
 int sock;
 struct sockaddr_in si;
 struct hostent *host;

 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  die("Couldn't get a socket!");

 bzero(&si, sizeof(si));

 host = gethostbyname(HOST);

 printf("addr = \"%s\"\nname = \"%s\"\n", 
  inet_ntoa(host->h_addr),
  host->h_name);

 si.sin_family = AF_INET;
 bcopy(host->h_addr, (char *) &si.sin_addr, host->h_length);
 si.sin_port = htons(80);

 if (connect(sock, (struct sockaddr*) &si, sizeof(si)) < 0)
  die("Eek! Couldn't connect!"); 

 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
  die("fcntl error!");

 sprintf(buf, "GET " PATH "\n\n");

 write(sock, buf, strlen(buf));

 for(;;) {
  n = read(sock, buf, 512);

  if (n < 0) {
   if (errno == EAGAIN) {
    errno = 0;
    continue;
   } 
   else {
    perror("read");
    break;
   }
  }
  else if (!n)
   die("all done!");
  else if (n) {
   write(0, buf, n);
  }
 }
}

void die(char *s)
{
 puts(s);
 exit(1);
}

Reply via email to