/* ******************************************************************************* ** FILE: BGdriver_UDP_client.C -- example program demonstrating usage of ** network programming library using BGDRIVER $QIO's ** ** PRODUCT: TCPware for VMS ** ** VERSION: V5.6 ** ** Copyright (c) 2001, 2002 by ** Process Software LLC ** Framingham, Massachusetts ** ** Copyright (c) 1997-1999 by ** Process Software Corporation ** Framingham, Massachusetts ** ** This software is furnished under a license for use on a ** single computer system and may be copied only 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 except for use on such ** system and to one who agrees to these license terms. Title ** to and ownership of the software shall at all times remain ** in Process Software LLC's name. ** ** The information in this document is subject to change ** without notice and should not be construed as a commitment ** by Process Software LLC. Process Software LLC assumes no ** responsibility for any errors that may appear in this document. ** ** ** ABSTRACT: ** ** BGdriver_UDP_server.c and BGdriver_UDP_client.c are a pair of example ** programs illustrating the use of datagrams via VMS's SYS$QIO system service ** calls to the UDPdriver. ** ** ** ** CLIENT SEQUENCE OF OPERATIONS: ** 1. Assign an I/O channel to UDP0: $ASSIGN; ** 2. Create & Bind send port: $QIO( IO$_SETMODE); ** 3. Exchange data: $QIO(IO$_WRITEVBLK), $QIO(IO$_READVBLK); ** 4. Deassign the channel: $DASSGN; ** ** ** BUILDING EXECUTABLES: ** ( /DEFINE=TCPWARE points to TCPware's ucx$inetdef.h instead of UCX's file) ** ** 1. with DECC: ** $ CC/PREFIX_LIBRARY_ENTRIES=ALL/DEFINE=TCPWARE BGDRIVER_UDP_CLIENT.C ** $ LINK BGDRIVER_UDP_CLIENT ** ** 2. with VAXC: ** $ CC/DEFINE=TCPWARE BGDRIVER_UDP_CLIENT.C ** $ LINK BGDRIVER_UDP_CLIENT, TCPWARE:UCX$IPC/LIB, SYS$INPUT/OPTIONS ** SYS$SHARE:VAXCRTL/SHARE ** ******************************************************************************* ** REQUEST: If you have comments, please send them to "support@process.com" ******************************************************************************* */ #include /* Standard C i/o */ #include #include /* VAX/VMS System services */ #include /* for STS$M_SUCCESS */ #include /* Descriptors */ #include /* VAX/VMS i/o definitions */ #include #include #include #ifdef TCPWARE #include "TCPWARE_INCLUDE:ucx$inetdef.h" /* Provided by TCPware install */ #else /* assume UCX */ #include /* Provided during UCX installation */ #endif /* ** Macros to check VMS success/failure status */ #define SUCCESS(status) (status & STS$M_SUCCESS) #define FAILURE(status) (!(status & STS$M_SUCCESS)) /* ** Service information */ #define SERVER_PORT 65000 #define SERVICE "test_discard" #define PROTO "udp" /* ** Values for boolean variables */ #define FALSE 0 #define TRUE 1 #if defined(__DECC) || defined(__DECCXX) #pragma __member_alignment __save #pragma __nomember_alignment #endif /* ** I/O status block */ struct IOSB_struct { short unsigned status; short unsigned byte_count; long dev_data; }; /* ** item list 2 descriptor used for remote host address ** (used in $QIO IO$_WRITEVBLK) */ struct item_list_2_struct { short complen; short itmcode; char *compadr; }; /* ** socket definition (used in $QIO IO$SETMODE) */ struct sockdef_struct { unsigned short protocol; unsigned char type; unsigned char domain; }; #if defined(__DECC) || defined(__DECCXX) #pragma __member_alignment __restore #endif /* ** function prototypes */ char *gets( char *); void exit( int); long sys$assign( struct dsc$descriptor_s *, unsigned short *, unsigned long, struct dsc$descriptor_s *); long sys$qiow( ); /* no prototyping due to varying P1 - P6 */ long sys$dassgn( unsigned short); main() { unsigned short channel; /* UDP channel number */ int status; /* For return status */ int continue_IO; /* boolean continue flag */ int got_host; char server_name[80]; int buflen; char buffer[1024]; struct hostent *hostent_P; struct servent *servent_P; struct IOSB_struct iosb; /* setup socket definition for QIO IO$SETMODE P1 arg */ static struct sockdef_struct sockdef = { UCX$C_UDP, /* UDP protocol */ INET_PROTYP$C_DGRAM, /* datagram type of socket */ UCX$C_AF_INET /* Internet domain */ }; /* local port & address used in $QIO IO$_SETMODE */ /* UCX's SOCKADDRIN is same as in.h's sockaddr_in Socket Address Structure*/ static struct SOCKADDRIN s_adr = { UCX$C_AF_INET, /* internetwork family */ 0, /* SIN$W_PORT */ 0, /* SIN$L_ADDR */ 0 /* SIN$T_ZERO */ }; /* local port & address used in $QIO IO$_SETMODE */ static struct item_list_2_struct s_adr_item2 = { sizeof( s_adr), 0, (char *) &s_adr }; /* remote port & address used in $QIO IO$_WRITEVBLK */ /* UCX's SOCKADDRIN is same as in.h's sockaddr_in Socket Address Structure*/ static struct SOCKADDRIN remote_s_adr = { UCX$C_AF_INET, /* internetwork family */ 0, /* SIN$W_PORT is setup below */ 0, /* SIN$L_ADDR is setup below */ 0 /* SIN$T_ZERO */ }; /* remote port & address used in $QIO IO$_SETMODE */ static struct item_list_2_struct remote_s_adr_item2 = { sizeof( remote_s_adr), 0, (char *) &remote_s_adr }; /* String descriptor for the UCX device BG0: */ static $DESCRIPTOR( UDP0_dev, "UCX$DEVICE"); /* Assign a channel to UCX$DEVICE to clone a new device */ status = sys$assign( &UDP0_dev, &channel, 0, 0); if (FAILURE( status) ) { printf("Failed to assign channel to UCX$DEVICE.\n"); exit( status); } /* setup port on cient: equivalent to BSD socket() & bind() functions. */ status = sys$qiow( 0, channel, IO$_SETMODE, &iosb, 0, 0, &sockdef, /* P1: socket definition */ 0, &s_adr_item2, /* P3: local socket */ 0, 0, 0); if ( SUCCESS( status) ) status = iosb.status; if ( FAILURE( status) ) { printf("Failed to open receive port\n"); exit( status); } /* ** Get server port number for named serice. ** If unknown, use default define. */ if ( (servent_P = getservbyname( SERVICE, PROTO)) != NULL) remote_s_adr.SIN$W_PORT = servent_P->s_port; else remote_s_adr.SIN$W_PORT = htons(SERVER_PORT); printf("Will connect to server port #: %d\n", ntohs( remote_s_adr.SIN$W_PORT) ); /* Query user for server name and get corresponding internet address. */ for (got_host = FALSE; got_host == FALSE; ) { printf("Enter name of remote host (e.g. localhost) : "); gets( server_name); if ((hostent_P = gethostbyname(server_name)) == NULL) { printf("Error, gethostbyname failed\n"); } else { got_host = TRUE; /* setup remote IP address for $QIO WRITEVBLK P3 argument */ remote_s_adr.SIN$L_ADDR = *( (long *)(hostent_P->h_addr) ); } } /* ** Keep getting input string & send to the server, till "CLOSE" is input. */ printf("\n\nKeep getting keyboard input & sending to the server,\n till ^Z (client exit) or 'CLOSE' (server exit) is input.\n\n"); for (continue_IO = TRUE; continue_IO; ) { /* ** Prepare message to send: ** Setup message (Replace with your application function.) */ printf("\nInput string to send. sends default: \n"); if ( gets( buffer) == NULL) break; buflen = strlen(buffer); /* if no input, setup default message */ if ( 0 == buflen) { strcpy( buffer, "User Datagram Protocol is described in RFC 768"); buflen = strlen(buffer); } /* ** Send the message */ status = sys$qiow( 0, channel, IO$_WRITEVBLK, &iosb, 0, 0, buffer, /* P1: buffer */ buflen, /* P2: buffer size */ &remote_s_adr_item2, /* P3: remote host address */ 0,0,0); if ( SUCCESS( status) ) status = iosb.status; if ( FAILURE( status) ) { printf("Error sending message. Status = %x\n", status ); break; } if ( 0 == strcmp( buffer, "CLOSE") || 0 == strcmp( buffer, "close") ) continue_IO = FALSE; } /* Deassign the channel */ status = sys$dassgn(channel); }