OpenVMS Source Code Demos

HTTP-CHECK.C

#define MODULE_NAME	HTTPCHECK
#define MODULE_IDENT	"V1.1"
/*===========================================================================
 *  Program:	HTTP-CHECK.C
 *  Author:	Hunter Goatley (Process Software Corporation)
 *  Date:	October 17, 2006
 *  Purpose:	A quick and dirty program to connect to a remote HTTP
 *		server, issue a HEAD command, and display the returned text.
 *  VMS Usage:	yada :== $CSMIS$USER3:[ADMCSM.NEIL]HTTP-CHECK.exe
 *		yada 127.0.0.1 80
 *===========================================================================
 */
#ifdef VMS
#ifdef __DECC
#pragma module MODULE_NAME MODULE_IDENT
#else
#ifdef vaxc
#module MODULE_NAME MODULE_IDENT
#endif /* vaxc */
#endif /* __DECC */
#include <in.h>
#include <ssdef.h>
#include <netdb.h>
#include <unixio.h>
#include <socket.h>
#define	EXITER(_sts)	return(_sts)
#else
#ifdef _WIN32
#include <winsock2.h>
#define SS$_ABORT 1

/* Define our macro to exit the appliction. */
#define	EXITER(_sts)	\
    WSACleanup() ;	\
    return(_sts) ;
#endif /* _WIN32 */
#endif /* VMS */
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int sock, x;
    struct sockaddr_in sock2_name;
    struct sockaddr_in inet_sa;
    struct hostent *hostentptr;
    char inbuff[2000];
    char target_host[255];
    char tmp[255];
    char *ptr;
    short wVer;
    int port;

#ifdef _WIN32
    /*
     *  For WinSock, you have to call WSAStartup first
     */
    WSADATA wsaData;
    wVer = MAKEWORD(2,0) ;              	// request WinSock version 2.0
    if (WSAStartup(wVer,&wsaData) != 0)  {  	// if startup failed
        perror("wsastartup") ;
	EXITER(SS$_ABORT) ;
    }
#endif
    /*
    **  Assign channel to 1st BG device.
    */
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
	perror("error creating socket");
	EXITER(SS$_ABORT);
	}

    if ((argc != 2) && (argc != 3))
	{
	printf ("Usage: http-check hostname [port]\n");
	exit (1);
	}

    strcpy (target_host, argv[1]);
    port = 80;
    if (argc == 3)
	{
	port = atoi(argv[2]);
	if (port == 0)
	    {
	    printf ("Invalid port number given\n");
	    exit(1);
	    }
	}

    printf ("Checking HTTP server on host %s, port %d....\n", target_host, port);

    if ((hostentptr = gethostbyname ((char *) target_host)) == NULL)
	{
	perror( "gethostbyname");
	EXITER(SS$_ABORT);
	}

    sock2_name.sin_family = hostentptr->h_addrtype;
    sock2_name.sin_port = htons(port);		/* HTTP server */
    sock2_name.sin_addr = * ((struct in_addr *) hostentptr->h_addr);
    /*
    **  Connect to the HTTP server
    */
    if (!(connect(sock, (struct sockaddr *)&sock2_name,
		sizeof (sock2_name))) == 0)
	{
	perror("connect");
	EXITER(SS$_ABORT);
	}

//  sprintf (tmp, "HEAD / HTTP/1.0\015\n\015\n");
//  sprintf (tmp, "HEAD / HTTP/1.0\r\n\r\n");
    sprintf (tmp, "GET  / HTTP/1.0\r\n\r\n");
    send (sock, (char *) tmp, strlen(tmp), 0);
    /*
    **  Wait for the response from our HTTP GET.
    */
    memset (inbuff, 0, sizeof(inbuff));
    x = recv (sock, (char *) &inbuff, sizeof(inbuff), 0);
    if (x < 0) {
	perror("recv");
	EXITER(SS$_ABORT);
	}
    printf("%s\n", inbuff);
    close(sock);
    EXITER(0) ;
}

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.