#pragma module tcpip$udp_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 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.1 * TCP/IP Services V5.0 or higher * * AUTHOR: * * TCPIP Development Group, CREATION DATE: 23-May-1989 * * -- */ /* Build, Configuration, and Run Instructions */ /* * BUILD INSTRUCTIONS: * * To build this example program use commands of the form, * * using the "C" compiler: * * $ cc/prefix=all TCPIP$UDP_CLIENT_SOCK.C * $ link TCPIP$UDP_CLIENT_SOCK * * * 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$udp_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$udp_client_sock * Enter remote host: * * Note: You can specify a server host by using either an IPv4 * address in dotted-decimal notation (e.g. 16.20.10.56) * or 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: a.b.c.d, 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 */ /* * FORWARD REFERENCES: */ int main( void ); /* client main */ void get_serv_addr( void * ); /* 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 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 sockaddr_in serv_addr; /* server socket address structure */ /* * init server's socket address structure */ memset( &serv_addr, 0, sizeof(serv_addr) ); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons( SERV_PORTNUM ); get_serv_addr( &serv_addr.sin_addr ); /* * create udp socket */ if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror( "Failed to create socket" ); exit( EXIT_FAILURE ); } /* * write datagram to server */ if ( sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 ) { perror( "Failed to write datagram to server" ); exit( EXIT_FAILURE ); } printf( "Sent a datagram to host: %s, port: %d\n", inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_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 address from the user and then * stores it in the server's socket address structure. Note that the * user can specify a server host by using either an IPv4 address in * dotted-decimal notation (e.g. 16.20.10.126) or a host domain name * (e.g. serverhost.hp.com). * * Enter "ctrl/z" to terminate program execution. * * SYNOPSIS: * * void get_serv_addr( void *addrptr ) * * FORMAL PARAMETERS: * * addrptr - pointer to socket address structure's 'sin_addr' field * to store the specified network address * * 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( void *addrptr ) { char buf[BUFSZ]; struct in_addr val; struct hostent *host; 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; val.s_addr = inet_addr( buf ); if ( val.s_addr != INADDR_NONE ) { memcpy( addrptr, &val, sizeof(struct in_addr) ); break; } if ( (host = gethostbyname(buf)) ) { memcpy( addrptr, host->h_addr, sizeof(struct in_addr) ); break; } } }