#pragma module tcpip$udp6_client_sock \ "V5.4-00" /* * © Copyright 1976, 2003 Hewlett-Packard Development Company, L.P. * * Confidential computer software. Valid license from HP and/or its * subsidiaries required for possession, use, or copying. * * Consistent with FAR 12.211 and 12.212, Commercial Computer Software, * Computer Software Documentation, and Technical Data for Commercial * Items are licensed to the U.S. Government under vendor's standard * commercial license. * * Neither HP nor any of its subsidiaries shall be liable for technical * or editorial errors or omissions contained herein. The information * in this document is provided "as is" without warranty of any kind * and is subject to change without notice. The warranties for HP * products are set forth in the express limited warranty statements * accompanying such products. Nothing herein should be construed as * constituting an additional warranty. * * ++ * FACILITY: * * EXAMPLES * * ABSTRACT: * * This is an example of a UDP/IP IPv4/IPv6 client using 4.x * BSD socket Application Programming Interface (API) to * handle network I/O operations. * * Refer to 'Build, Configuration, and Run Instructions' for * details on how to build, configure, and run this program. * * ENVIRONMENT: * * OpenVMS Alpha/VAX V7.2-2 * TCP/IP Services V5.4 or higher * * AUTHOR: * * TCPIP Development Group, CREATION DATE: 10-Jan-2003 * * -- */ /* Build, Configuration, and Run Instructions */ /* * BUILD INSTRUCTIONS: * * To build this example program use commands of the form, * * using the "C" compiler: * * $ cc/define=(_SOCKADDR_LEN)/include=TCPIP$EXAMPLES: - * TCPIP$UDP6_CLIENT_SOCK.C * $ link TCPIP$UDP6_CLIENT_SOCK, TCPIP$LIBRARY:TCPIP$LIB/library * * * CONFIGURATION INSTRUCTIONS: * * No special configuration required. * * * RUN INSTRUCTIONS: * * To run this example program: * * 1) Start the client's server program as shown below: * * $ run tcpip$udp6_server_sock * Waiting for a client datagram on port: m * * 2) After the server program blocks, start this client program, * entering the server host as shown below: * * $ run tcpip$udp6_client_sock * Enter remote host: * * Note: You can specify a server host using any * of the following: * * o An IPv4 address in dotted-decimal notation * (e.g. 16.20.10.56) * * o An IPv6 address in hexadecimal * (e.g. 1070:0:0:0:0:800:200C:417B) * * o An IPv4-mapped IPv6 address in hexadecimal * (e.g. ::ffff:16.20.10.56) * * o A host domain name * (e.g. serverhost.hp.com) * * 3) The client program then displays server address information * and server data as show below: * * Sent a datagram to host: x, port: n * Data sent: Hello, world! * * You can enter "ctrl/z" at any user prompt to terminate program * execution. * */ /* * INCLUDE FILES: */ #include /* define internet related constants, */ /* functions, and structures */ #include /* define network address info */ #include /* define network database library info */ #include /* define BSD 4.x socket api */ #include /* define standard i/o functions */ #include /* define standard library functions */ #include /* define string handling functions */ #include /* define unix i/o */ /* * NAMED CONSTANTS: */ #define BUFSZ 1024 /* user input buffer size */ #define SERV_PORTNUM "12345" /* server port number string */ /* * FORWARD REFERENCES: */ int main( void ); /* client main */ void get_serv_addr( struct addrinfo *hints, struct addrinfo **res ); /* get server host address */ /* Client Main */ /* * FUNCTIONAL DESCRIPTION: * * This is the client's main-line code. It handles all the tasks of the * client including: socket creation, writing server data, and deleting * the socket. * * This example program implements a typical UDP IPv4/IPv6 client using * the BSD socket API to handle network i/o operations as shown below: * * 1) To create a socket: * * socket() * * 2) To transfer data: * * sendto() * * 3) To close a socket: * * close() * * This function is invoked by the DCL "RUN" command (see below); the * function's completion status is interpreted by DCL and if needed, * an error message is displayed. * * SYNOPSIS: * * int main( void ) * * FORMAL PARAMETERS: * * ** None ** * * IMPLICIT INPUTS: * * ** None ** * * IMPLICIT OUTPUTS: * * ** None ** * * FUNCTION VALUE: * * completion status * * SIDE EFFECTS: * * ** None ** * */ int main( void ) { int sockfd; /* udp socket descriptor */ char buf[] = "Hello, world!"; /* client data buffer */ struct addrinfo hints; /* input values to direct operation */ struct addrinfo *res; /* linked list of addrinfo structs */ /* * init server's addrinfo structure */ memset( &hints, 0, sizeof(hints) ); hints.ai_family = AF_INET6; hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED | AI_CANONNAME; hints.ai_protocol = IPPROTO_TCP; hints.ai_socktype = SOCK_STREAM; get_serv_addr( &hints, &res ); /* * create udp socket */ if ( (sockfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ) { perror( "Failed to create socket" ); exit( EXIT_FAILURE ); } /* * write datagram to server */ if ( sendto(sockfd, buf, sizeof(buf), 0, res->ai_addr, res->ai_addrlen) < 0 ) { perror( "Failed to write datagram to server" ); exit( EXIT_FAILURE ); } printf( "Sent a datagram to host: %s, port: %d\n", res->ai_canonname, htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_port) ); printf( "Data sent: %s\n", buf ); /* output data buffer */ /* * close udp socket */ if ( close(sockfd) < 0 ) { perror( "Failed to close socket" ); exit( EXIT_FAILURE ); } exit( EXIT_SUCCESS ); } /* Get Server Host Address */ /* * FUNCTIONAL DESCRIPTION: * * This function gets the server host's name or address from the user * and then stores it in the addrinfo structure. * * Enter "ctrl/z" to terminate program execution. * * SYNOPSIS: * * void get_serv_addr( struct addrinfo *hints, struct addrinfo **res ) * * FORMAL PARAMETERS: * * hints - pointer to structure containing input values that may * direct the operation by providing options and by * limiting the returned information to a specific socket * type, address family and/or protocol * * res - upon successful return of getaddrinfo, the pointer to * a location for the linked list of addrinfo structures * * IMPLICIT INPUTS: * * ** None ** * * IMPLICIT OUTPUTS: * * ** None ** * * FUNCTION VALUE: * * ** None ** * * SIDE EFFECTS: * * Program execution is terminated if unable to read user's input. * */ void get_serv_addr( struct addrinfo *hints, struct addrinfo **res ) { int gai_error; /* return value of getaddrinfo() */ char buf[BUFSZ]; /* input data buffer */ const char *port = SERV_PORTNUM; /* server port number */ while ( TRUE ) { printf( "Enter remote host: " ); if ( fgets(buf, sizeof(buf), stdin) == NULL ) { printf( "Failed to read User input\n" ); exit( EXIT_FAILURE ); } buf[strlen(buf)-1] = 0; gai_error = getaddrinfo( buf, port, hints, res ); if ( gai_error ) printf( "Failed to resolve name or address: %s\n", gai_strerror(gai_error) ); else break; } }