Everhart,Glenn From: sander@vmsweb.enet.dec.com Sent: Tuesday, April 07, 1998 3:47 PM To: Info-VAX@Mvb.Saic.Com Subject: RE: HTTP Connection Outside of Browser Environment RE: program to open an HTTP connection.... /* * hostget.c * * a program to test http servers * * usage: hostget hostname portnumber "string" * * eg: hostget www.openvms.digital.com 80 "GET /index.html" * (if on internal network) or... * hostget www-proxy.server.com 8080 "http://www.openvms.digital.com/index.html" * * */ #include #include #include #include #include #include #include #include #include #include #include #include #include "strutil.h" #define MAXDATASIZE 7500 void cleanup (int shut, int socket); void dump (char * str, int len, int dstart); int main (int argc, char *argv[]) { int sock_1; /* socket */ char message[512]; char *str_ptr, *cur_ptr; static struct sockaddr_in sock2_name; /* Address struct for socket2.*/ struct hostent hostentstruct; /* Storage for hostent data. */ struct hostent *hostentptr; /* Pointer to hostent data. */ static char hostname[256]; /* Name of local host. */ int flag; int retval; /* helpful for debugging */ int shut = FALSE; /* flag to cleanup */ char buf[MAXDATASIZE], *bptr; int n, numbytes, databytes, dumpstart, buflen; /* * Check input parameters. */ if (argc != 4 ) { printf("Usage: hostget hostname portnumber \"string\".\n"); exit(1); } strcpy(message, "\0"); if (! strcasestr (argv[3], "GET ")) { strcat (message, "GET "); } strcat(message, argv[3]); if (! strcasestr (message, "HTTP/1.0")) { strcat (message, " HTTP/1.0"); } strcat(message, "\n\n"); /* * Open socket 1: AF_INET, SOCK_STREAM. */ if ((sock_1 = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror( "socket"); exit(1); } /* *Get pointer to network data structure for socket 2 (remote host). */ if ((hostentptr = gethostbyname (argv[1])) == NULL) { perror( "gethostbyname"); cleanup(shut, sock_1); } /* * Copy hostent data to safe storage. */ hostentstruct = *hostentptr; /* * Fill in the name & address structure for socket 2. */ printf("Using port # %i\n", atoi(argv[2])); sock2_name.sin_family = hostentstruct.h_addrtype; sock2_name.sin_port = htons(atoi(argv[2])); sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr); /* * Connect socket 1 to sock2_name. */ retval = connect(sock_1, (struct sockaddr *) &sock2_name, sizeof (sock2_name)); if (retval) { perror("connect"); cleanup(shut, sock_1); } /* * Send message to socket 2. */ flag = 0; /* maybe 0 or MSG_OOB */ retval = send(sock_1, message , strlen(message), flag); if (retval < 0) { perror ("send"); shut = TRUE; } bptr = buf; buflen = MAXDATASIZE; numbytes = 0; while ((n = read(sock_1, bptr, buflen)) > 0) { bptr += n; buflen -= n; numbytes += n; } *bptr = 0; databytes = numbytes; str_ptr = strstr (buf, "\r\n\r\n"); cur_ptr = buf; if (str_ptr > cur_ptr) { databytes = numbytes - (str_ptr - cur_ptr); printf("\n"); for (cur_ptr; cur_ptr != str_ptr; cur_ptr++) { printf ("%c", *cur_ptr); } printf("\n"); } dumpstart = numbytes - databytes + 4; dump(buf, numbytes, dumpstart); /* * Call cleanup to shutdown and close socket. */ cleanup(shut, sock_1); return 0; } /* end main */ void dump (char * str, int len, int dstart) { int i, tmpcnt, icnt; char tmpbuf[17], t; icnt = 0; for (i = dstart; i < len; i++) { tmpcnt = icnt & 0xf; if (tmpcnt == 0) { printf("%04x: ", (unsigned short) icnt); strcpy(tmpbuf, " "); } icnt = icnt + 1; t = str[i]; if (t < 32 || t > 127) t = '.'; tmpbuf[tmpcnt] = t; printf("%02x ", (unsigned char) str[i]); if (tmpcnt == 15) { tmpbuf[16] = 0; printf(" %s\n", tmpbuf); } } if (tmpcnt != 15) { for (i = tmpcnt; i < 15; i++) printf(" "); printf(" %s\n", tmpbuf); } } void cleanup(int shut, int socket) { int retval; /* * Shutdown socket completely -- only if it was connected */ if (shut) { retval = shutdown(socket,2); if (retval == -1) perror ("shutdown"); } /* * Close socket. */ retval = close (socket); if (retval) perror ("close"); } -- ------------------------------------------------------------------ Warren Sander OpenVMS Marketing Digital Equipment Corporation Work: warren.sander@digital.com 129 Parker Street PK03-2/T20 Personal: sander@ultranet.com Maynard, MA 01754 (508) 493-5470/0084 voice/fax My opinions are my own and I only speak for myself Read http://www.openvms.digital.com/ ------------------------------------------------------------------