OpenVMS Source-Code Demos

TCPIP$TCP_CLIENT_QIO-DECC.C

/*====================================================================
*
*			  COPYRIGHT (C) 1989, 1998 BY
*	      DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS.
*
* This software is furnished under a license and may be used and  copied
* only  in  accordance  with  the  terms  of  such  license and with the
* inclusion of the above copyright notice.  This software or  any  other
* copies  thereof may not be provided or otherwise made available to any
* other person.  No title to and ownership of  the  software  is  hereby
* transferred.
*
* The information in this software is subject to change  without  notice
* and  should  not  be  construed  as  a commitment by DIGITAL EQUIPMENT
* CORPORATION.
*
* DIGITAL assumes no responsibility for the use or  reliability  of  its
* software on equipment which is not supplied by DIGITAL.
*
*
*
*  FACILITY:
*	INSTALL
*
*
*  ABSTRACT:
*	This is an example of a TCP/IP client using the QIO
*	interface.
*
*
*  ENVIRONMENT:
*	TCPIP V5.0 or higher
*
*  BUILD INSTRUCTIONS:
*
*	To link in VAXC/VMS you must have the following
*	entries in your .opt file:
*	   sys$library:tcpip$ipc.olb/lib
*	   sys$share:vaxcrtl.exe/share
*
*	For DEC C or DEC C++, compile /PREFIX=ALL and link via
*          $ link TCPIP$TCP_CLIENT_QIO
*
*    To build this example program use commands of the form,
*
*        using the DEC "C" compiler:
*
*            $ cc/prefix=all TCPIP$TCP_CLIENT_QIO.C
*            $ link TCPIP$TCP_CLIENT_QIO
*
*        using the DEC "C++" compiler:
*
*            $ cxx/prefix=all/define=VMS TCPIP$TCP_CLIENT_QIO.C
*            $ link TCPIP$TCP_CLIENT_QIO
*
*        using the VAX "C" compiler:
*
*            $  cc /vaxc TCPIP$TCP_CLIENT_QIO.C
*            $  link TCPIP$TCP_CLIENT_QIO, -
*                    SYS$LIBRARY:TCPIP$IPC/LIB, -
*                    SYS$INPUT/OPTIONS
*            SYS$SHARE:TCPIP$IPC_SHR/SHARE
*            SYS$SHARE:VAXCRTL.EXE/SHARE
*
*  AUTHORS:
*	TCPIP Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*       16 May 1996 Joseph J. Vlcek
*       Make compatible with the DEC C and DEC C++ compilers.
*       Add directions on how to build this example modules.
*
*       5 Nov 1997 Immi Mohammed
*       Changed ucx$inetdef.h to shrlib$:inet_user.h
*
*       31 Mar 1998 Jason Fountain
*       Initialized retval
*       Changed shrlib$:inet_user.h to tcpip$inetdef.h
*/

#include <descrip.h>        /* VMS descriptor stuff */
#include <iodef.h>          /* I/O FUNCTION CODE DEFS */
#include <in.h>             /* internet system Constants and structures. */
#include <ssdef.h>          /* SS$_<xyz> sys ser return stati <8-) */
#include <starlet.h>        /* Sys ser calls */
#include <stdio.h>          /* UNIX 'Standard I/O' Definitions   */
#include <stdlib.h>         /* General Utilities */
#include <string.h>         /* String handling function definitions */
#include <tcpip$inetdef.h>    /* TCPIP network definitions */

/* Convert short port number from host to network byte order */
#define htons(x) ((unsigned short)((x<<8)|(x>>8)))

main() {
	int	status;		/* For return status */
	short	channel;	/* INET channel */
	short	sck_parm[2];	/* Socket creation parameter */
	short	iosb[4];	/* I/O status block */

	char	buf[512] = "Hi there";
	int	buflen = 512;		/* buffer length */
	int	retval = 0;
	char	junk[256], dot, *dummy;	/* used for scanf */
	short	port;
	struct	sockaddr_in remote_host;
//----------
//	struct	dsc$descriptor inet_dev =
//		{10, DSC$K_CLASS_S, DSC$K_DTYPE_T, "TCPIP$DEVICE"};	/* original (broken) code */
//----------
//	struct	dsc$descriptor inet_dev =
//		{12, DSC$K_CLASS_S, DSC$K_DTYPE_T, "TCPIP$DEVICE"};	/* fixed code */
//----------
	$DESCRIPTOR(  inet_dev, "TCPIP$DEVICE:"  );			/* better fixed code */
//----------
	struct	IL2 {
		unsigned int il2_length;
		char *il2_address;
	} rhst_adrs;

	rhst_adrs.il2_length  = sizeof( remote_host );
	rhst_adrs.il2_address = (char*) &remote_host;

	sck_parm[0] = TCPIP$C_TCP;		/* TCP/IP protocol */
	sck_parm[1] = INET_PROTYP$C_STREAM;	/* stream type of socket */

	remote_host.sin_family = TCPIP$C_AF_INET;	/* INET family */
	dummy = (char*)&remote_host.sin_addr;
	while (retval != 7) {
		printf ("Enter remote host Internet address (a.b.c.d):\n");
		retval = scanf("%d %c %d %c %d %c %d",
			&dummy[0], &dot, &dummy[1], &dot,
			&dummy[2], &dot, &dummy[3]);
		if (retval != 7)
			scanf("%s",junk);	/* discard bad input */
	}

	while (retval != 1) {
		printf("Enter remote port number:\n");
		retval = scanf("%hd", &port);
		if (retval == 1)
			remote_host.sin_port = htons(port);
		else scanf("%s",junk);	/* discard bad input */
	}

	/* Assign a channel to the TCPIP device. */
	status = sys$assign(&inet_dev, &channel, 0, 0);
	if (!(status & 1)) {
		printf("Failed to assign channel to TCPIP device.\n");
		exit(status);
	}

	/* Create the socket */
	status = sys$qiow(3,		/* Event flag */
			channel,	/* Channel number */
			IO$_SETMODE,	/* I/O function */
			iosb,		/* I/O status block */
			0, 0,
			&sck_parm, 0,	/* P1 Socket creation parameter */
			0, 0,
			0, 0);
	if (status & 1) status = iosb[0];
	if (!(status & 1)) {
		printf("Failed to create and bind the device socket.\n");
		exit(status);
	}

	/* Connect to specified host and port number */
	status = sys$qiow(3,		/* Event flag */
			channel,	/* Channel number */
			IO$_ACCESS,	/* I/O function */
			iosb,		/* I/O status block */
			0, 0,
			0, 0,
			&rhst_adrs,	/* P3 Remote IP address */
			0, 0, 0);
	if (status & 1) status = iosb[0];
	if (!(status & 1)) {
		printf("Failed to connect to remote host.\n");
		exit(status);
	}

	/* Write I/O buffer. */
	status = sys$qiow(3,		/* Event flag */
			channel,	/* Channel number */
			IO$_WRITEVBLK,	/* I/O function */
			iosb,		/* I/O status block */
			0, 0,
			buf,		/* P1 buffer */
			buflen,		/* P2 buffer length */
			0, 0, 0, 0);
	if (status & 1) status = iosb[0];
	if (!(status & 1))
		printf("Failed to write to socket.\n");

	/* Shut down the socket (optional). */
	status = sys$qiow(3,
			channel,
			IO$_DEACCESS|IO$M_SHUTDOWN,
			iosb,
			0, 0,
			0, 0, 0,
			TCPIP$C_DSC_ALL,	/* P4 Discard all packets */
			0, 0);
	if (status & 1) status = iosb[0];
	if (!(status & 1))
		printf("Failed to shut down the socket.\n");

	/* Close the socket (optional). */
	status = sys$qiow(3,
			channel,
			IO$_DEACCESS,
			iosb,
			0, 0,
			0, 0, 0, 0, 0, 0);
	if (status & 1) status = iosb[0];
	if (!(status & 1))
		printf("Failed to close the socket.\n");

	/* Deassign the TCPIP device channel. */
	status = sys$dassgn(channel);
	if (!(status & 1))
		printf("Failed to deassign the channel.\n");
}

Back to OpenVMS
Back to OpenVMS Demo Index
Back to Home
Neil Rieck
Kitchener - Waterloo - Cambridge, Ontario, Canada.