Here is a small C program using the Berkeley Sockets API to communicate with a web server via TCP/IP. It connects to a given server, sends a very simple http request and retrieves the resulting response for displaying it to the user. Control characters in the web server output are shown in the form $xx (for example $d for ASCII 13, which is CR). Not quite fancy or well-written, but at least a starter for libsocket use. shell/iX> cat webreq.c /* simple socket client. lars appel 15.nov.98 connects to web server and requests data. usage: webreq ip-addr port "HEAD /urlpart" webreq ip-addr port "GET /urlpart" example: webreq 127.0.0.1 80 "HEAD /" */ #include #include #include #include #include #include main(int argc, char *argv[]) { int sd; struct sockaddr_in sa; int ret; char buf[4096]; int len; int k; int port; if (argc < 2) { printf ("missing target ip\n"); exit (5); } else printf ("using target ip %s\n", argv[1]); if (argc < 3) { printf ("missing target port\n"); exit (6); } else { port = atoi (argv[2]); printf ("using target port %i\n", port); } if (argc < 4) { printf ("missing request string\n"); exit (7); } else printf ("using request string %s\n", argv[3]); sd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) exit (1); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(argv[1]); sa.sin_port = port; ret = connect (sd, &sa, sizeof(sa)); if (ret != 0) exit (2); strcpy (buf, argv[3]); strcat (buf, " HTTP/1.0\r\n\r\n"); printf ("actual request sent is %s", buf); len = send (sd, buf, strlen(buf), 0); if (len == -1) exit (3); len = recv (sd, buf, sizeof(buf), 0); if (len == -1) exit (4); close (sd); for (k = 0; k < len; k++) { if (buf[k] >= 32 && buf[k] < 127) printf ("%c",buf[k]); else if (buf[k] == '\n') printf ("\n"); else printf (" $%x ",buf[k]); } } Here we compile with c89 and libsocket.a -- GNU gcc should also work. shell/iX> c89 -D_POSIX_SOURCE -D_SOCKET_SOURCE webreq.c -lsocket -o webreq shell/iX> ls -l total 255 -rwxrwx--- 1 LARS.APPEL APPEL 126720 Apr 30 18:45 webreq -rw-r----- 1 LARS.APPEL APPEL 1510 Apr 30 18:42 webreq.c -rw-rw---- 1 LARS.APPEL APPEL 2048 Apr 30 18:45 webreq.o And now let's see two examples for using this little program, the first one gets a positive response, the second one gets a negative response from the web server (because the specified web page was not found)... shell/iX> webreq 12.34.56.78 8008 "HEAD /index.html" using target ip 12.34.56.78 using target port 8008 using request string HEAD /index.html actual request sent is HEAD /index.html HTTP/1.0 HTTP/1.1 200 OK $d Date: Sun, 30 Apr 2000 16:49:15 GMT $d Server: Apache/1.3.9 (MPE/iX) ApacheJServ/1.1 $d Last-Modified: Sat, 25 Mar 2000 15:48:08 GMT $d ETag: "4788-3a2-38dcdfb8" $d Accept-Ranges: bytes $d Content-Length: 930 $d Connection: close $d Content-Type: text/html $d $d shell/iX> webreq 12.34.56.78 8008 "HEAD /nonsense.txt" using target ip 12.34.56.78 using target port 8008 using request string HEAD /nonsense.txt actual request sent is HEAD /nonsense.txt HTTP/1.0 HTTP/1.1 404 Not Found $d Date: Sun, 30 Apr 2000 16:49:45 GMT $d Server: Apache/1.3.9 (MPE/iX) ApacheJServ/1.1 $d Connection: close $d Content-Type: text/html $d $d shell/iX> exit