#pragma module tcpip$udp6_server_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 server 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_SERVER_SOCK.C * $ link TCPIP$UDP6_SERVER_SOCK, TCPIP$LIBRARY:TCPIP$LIB/library * * * CONFIGURATION INSTRUCTIONS: * * No special configuration required. * * * RUN INSTRUCTIONS: * * To run this example program: * * 1) Start this server program server as shown below: * * $ run tcpip$udp6_server_sock * Waiting for a client datagram on port: m * * 2) After the server program blocks, start the 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 server program then displays client address information * and client data as show below: * * Received a datagram from host: x (a), port: n * Data received: 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 SERV_PORTNUM 12345 /* server port number */ /* * FORWARD REFERENCES: */ int main( void ); /* server main */ /* Server Main */ /* * FUNCTIONAL DESCRIPTION: * * This is the server's main-line code. It handles all the tasks of the * server including: socket creation, binding a socket to the server's * internet address and port, and reading client data. * * This example program implements a typical UDP IPv4 server using the * BSD socket API to handle network i/o operations as shown below: * * 1) To create a socket: * * socket() * * 2) To set REUSEADDR socket option: * * setsockopt() * * 3) To bind internet address and port number to a socket: * * bind() * * 4) To transfer data: * * recvfrom() * * 5) 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 ) { char buf[512]; /* server data buffer */ int optval = 1; /* SO_REUSEADDR'S option value (on) */ int sockfd; /* socket descriptor */ int gni_error; /* return status for getnameinfo() */ unsigned int client_addrlen; /* returned length of client socket */ /* address structure */ struct sockaddr_in6 client_addr; /* client socket address structure */ struct sockaddr_in6 serv_addr; /* server socket address structure */ char node[NI_MAXHOST]; /* buffer to receive node name */ char port[NI_MAXHOST]; /* buffer to receive port number */ char addrbuf[INET6_ADDRSTRLEN]; /* buffer to receive host's address */ /* * init client's socket address structure */ memset( &client_addr, 0, sizeof(client_addr) ); /* * init server's socket address structure */ memset( &serv_addr, 0, sizeof(serv_addr) ); serv_addr.sin6_family = AF_INET6; serv_addr.sin6_port = htons( SERV_PORTNUM ); serv_addr.sin6_addr = in6addr_any; /* * create a udp socket */ if ( (sockfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ) { perror( "Failed to create socket" ); exit( EXIT_FAILURE ); } /* * bind server's internet address and port number to socket */ if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0 ) { perror( "Failed to set socket option" ); exit( EXIT_FAILURE ); } if ( bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 ) { perror( "Failed to bind socket" ); exit( EXIT_FAILURE ); } /* * read datagram from client */ printf( "Waiting for a client datagram on port: %d\n", ntohs(serv_addr.sin6_port) ); client_addrlen = sizeof(client_addr); if ( recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *) &client_addr, &client_addrlen) < 0 ) { perror( "Failed to read datagram from client" ); exit( EXIT_FAILURE ); } gni_error = getnameinfo( (struct sockaddr *)&client_addr, client_addrlen, node, sizeof(node), NULL, 0, NI_NAMEREQD ); if ( gni_error ) { printf( "Failed to translate client address: %s\n", gai_strerror(gni_error) ); exit( EXIT_FAILURE ); } gni_error = getnameinfo( (struct sockaddr *)&client_addr, client_addrlen, addrbuf, sizeof(addrbuf), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV ); if ( gni_error ) { printf( "Failed to translate client address and/or port: %s\n", gai_strerror(gni_error) ); exit( EXIT_FAILURE ); } printf( "Received a datagram from host: %s (%s), port: %s\n", node, addrbuf, port ); printf( "Data received: %s\n", buf ); /* output client's data buffer */ /* * close udp socket */ if ( close(sockfd) < 0 ) { perror( "Failed to close socket" ); exit( EXIT_FAILURE ); } exit( EXIT_SUCCESS ); }