#pragma module tcpip$tcp_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 TCP/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$TCP_CLIENT_SOCK.C * $ link TCPIP$TCP_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$tcp_server_sock * Waiting for a client connection on port: m * * 2) After the server program blocks, start this client program, * entering the server host as shown below: * * $ run tcpip$tcp_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 connection information * and server data as shown below: * * Initiated connection to host: a.b.c.d, 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 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, initiating server connections, * reading server connection data, and terminating server connections. * * This example program implements a typical TCP IPv4 client using the * BSD socket API to handle network i/o operations as shown below: * * 1) To create a socket: * * socket() * * 2) To initiate a connection: * * connect() * * 3) To transfer data: * * recv() * * 4) To shutdown a socket: * * shutdown() * * 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 ) { int sockfd; /* connection socket descriptor */ char buf[512]; /* 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 connection socket */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { perror( "Failed to create socket" ); exit( EXIT_FAILURE ); } /* * connect to specified host and port number */ printf( "Initiated connection to host: %s, port: %d\n", inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_port) ); if ( connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 ) { perror( "Failed to connect to server" ); exit( EXIT_FAILURE ); } /* * connection established with a server; * now attempt to read on this connection */ if ( recv(sockfd, buf, sizeof(buf), 0) < 0 ) { perror( "Failed to read data from server connection" ); exit( EXIT_FAILURE ); } printf( "Data received: %s\n", buf ); /* output client's data buffer */ /* * shutdown connection socket (both directions) */ if ( shutdown(sockfd, 2) < 0 ) { perror( "Failed to shutdown server connection" ); exit( EXIT_FAILURE ); } /* * close connection 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; } } }