/* ******************************************************************************* ** FILE: UDPdriver_server.C -- example program demonstrating usage of ** network programming library using UDPDRIVER $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: ** ** UDPdriver_server.c and UDPdriver_client.c are a pair of example programs ** illustrating the use of datagrams via VMS's SYS$QIO system service ** calls to the UDPdriver. ** ** ** ** SERVER SEQUENCE OF OPERATIONS: ** 1. Assign an I/O channel to UDP0: $ASSIGN; ** 2. Open receive port: $QIO(IO$_SETMODE|IO$M_CTRL|IO$M_STARTUP); ** 3. Exchange data: $QIO(IO$_WRITEVBLK), $QIO(IO$_READVBLK); ** 4. Close port: $QIO(IO$_SETMODE|IO$M_CTRL|IO$M_SHUTDOWN); ** 5. Deassign the channel: $DASSGN; ** ** ** BUILDING EXECUTABLES: ** ** 1. on VAX : ** with VAXC: ** $ CC UDPDRIVER_SERVER.C ** $ LINK UDPDRIVER_SERVER, TCPWARE:UCX$IPC/LIB, SYS$INPUT/OPTIONS ** SYS$SHARE:VAXCRTL/SHARE ** ** Alternatively TCPware's socket library can be used: ** $ LINK UDPDRIVER_SERVER, SYS$INPUT/OPTIONS ** SYS$SHARE:TCPWARE_SOCKLIB_SHR/SHARE ** SYS$SHARE:VAXCRTL/SHARE ** ** with DECC: ** $ CC/DECC/PREFIX_LIBRARY_ENTRIES=ALL UDPDRIVER_SERVER.C ** $ LINK UDPDRIVER_SERVER ** ** 2. on ALPHA: ** $ CC/PREFIX_LIBRARY_ENTRIES=ALL UDPDRIVER_SERVER.C ** $ LINK UDPDRIVER_SERVER ** ******************************************************************************* ** 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 /* ** 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; }; /* ** Extended characteristics buffer structure, i.e. ecb structure */ struct ecb_struct { short para_id; long para_val; }; /* ** address buffer structure */ struct addr_struct { unsigned long src_addr; unsigned short src_port; /* ignored w/ WRITEVBLK */ short null_1; unsigned long dest_addr; unsigned short dest_port; short null_2; }; #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 */ unsigned short port; /* UDP port number */ int status; /* For return status */ int continue_IO; /* boolean $qio status */ char buffer[1024]; struct servent *servent_P; struct IOSB_struct iosb; struct addr_struct read_addr; struct ecb_struct ecb[1]; struct dsc$descriptor ecb_dsc; /* extended char buffer descriptor */ struct in_addr remote_in_addr; struct hostent *hostent_P; /* String descriptor for the UDP0 device */ static $DESCRIPTOR( UDP0_dev, "UDP0"); /* Assign a channel to UDP0 to clone a new device */ status = sys$assign(&UDP0_dev, &channel, 0, 0); if (FAILURE( status) ) { printf("Failed to assign channel to device UDP0.\n"); exit( status); } /* ** Get server port number for named serice. ** If unknown ( == 0 or = -1), use default define. */ if ( (int)(servent_P = getservbyname( SERVICE, PROTO)) > 0) port = servent_P->s_port; else port = SERVER_PORT; printf("Will receive on server port #: %d\n\n", port); /* ** Passive open a receive port on server */ ecb[0].para_id = 1; /* Local port number */ ecb[0].para_val = port; ecb_dsc.dsc$w_length = sizeof(ecb[0]); ecb_dsc.dsc$b_dtype = 0; ecb_dsc.dsc$b_class = 0; ecb_dsc.dsc$a_pointer = (char *)&ecb; status = sys$qiow( 0, channel, (IO$_SETMODE|IO$M_CTRL|IO$M_STARTUP), &iosb, 0, 0, 0, /* P1 */ &ecb_dsc, /* P2: extended char buffer */ 0,0,0,0); if ( SUCCESS( status) ) status = iosb.status; if ( FAILURE( status) ) { printf("Failed to open receive port\n"); exit( status); } /* ** Keep receiving messages, till "CLOSE" is input. */ for (continue_IO = TRUE; continue_IO; ) { status = sys$qiow( 0, channel, IO$_READVBLK, &iosb, 0, 0, buffer, /* P1: buffer */ sizeof(buffer), /* P2: buffer size */ &read_addr, /* P3: adr of src/dest info buf */ 0,0,0); if ( SUCCESS( status) ) status = iosb.status; if ( FAILURE( status) ) { printf("Error reading message. Status = %x\n", status ); continue; } /* ** Use remote IP address to query DNS for host name. ** If unknown, just show remote IP address. */ remote_in_addr.s_addr = read_addr.src_addr; hostent_P = gethostbyaddr( (char *) &remote_in_addr.s_addr, 4, AF_INET); if (hostent_P) printf("\nClient data received from %s = %s\n", inet_ntoa( remote_in_addr), hostent_P->h_name); else printf("\nClient data received from [%s]\n", inet_ntoa( remote_in_addr) ); buffer[ iosb.byte_count] = '\0'; /* terminate received string */ printf("Received: %s\n", buffer); /* display the message received */ if ( 0 == strcmp( buffer, "CLOSE") || 0 == strcmp( buffer, "close") ) continue_IO = FALSE; } /* ** Close the receive port */ status = sys$qiow(0, channel, (IO$_SETMODE|IO$M_CTRL|IO$M_SHUTDOWN), &iosb, 0,0, 0,0,0,0,0,0); if ( SUCCESS( status) ) status = iosb.status; if ( FAILURE( status) ) { printf("Error closing port.\n"); exit( status); } /* Deassign the channel */ status = sys$dassgn(channel); }