Cleansocks Example: The simple web client example from the
text, ported to Cleansocks.
/* This is Comer's CNAI webclient.c example ported to use cleansocks. */
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cleansocks.h>
#include <cleanip.h>
/*-----------------------------------------------------------------------
*
* Program: webclient
* Purpose: fetch page from webserver and dump to stdout with headers
* Usage: webclient <hostname> <path> [portnum]
* Note: Portnum is optional. If not specified the standard www appnum
* (80) is used.
*
*-----------------------------------------------------------------------
*/
using namespace std;
using namespace cleansocks;
static const int BUFFSIZE = 256;
int main(int argc, char *argv[])
{
if(argc < 3 || argc > 4) {
cerr << "Usage: " << argv[0] << " <hostname> <path> [portnum]"
<< endl;
exit(1);
}
/* Convert host name and get port number. */
IPaddress comp = lookup_host(argv[1]);
IPport app;
if(argc == 4)
app = atoi(argv[3]);
else
app = lookup_service("www");
/* Connect to the web server */
TCPsocket conn;
connect(conn, IPendpoint(comp, app));
/* Send an HTTP/1.0 request to the webserver. I could have built
this using C++ strings, but I kept the original to show a send
of the buffer/size form. */
char buff[BUFFSIZE];
int len = sprintf(buff, "GET %s HTTP/1.0\r\n\r\n", argv[2]);
send(conn, buff, len);
/* Dump all data received from the server to stdout */
while((len = recv(conn, buff, BUFFSIZE)) > 0)
cout.write(buff, len);
}