#pragma module tcpip$tcp_server_sock_auxs \ "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 server using 4.x BSD * socket Application Programming Interface (API) to handle * network I/O operations. In addition, it shows how to * accept connections from the auxiliary server. * * 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_SERVER_SOCK_AUXS.C * $ link TCPIP$TCP_SERVER_SOCK_AUXS * * * CONFIGURATION INSTRUCTIONS: * * To configure this example program: * * 1) Create a service run command procedure, named HELLO_RUN.COM, that * contains the following lines: * * $ define sys$output ddcu:[directory]hello_service.log * $ define sys$error ddcu:[directory]hello_service.log * $ run ddcu:[directory]tcpip$tcp_server_sock_auxs.exe * * where: ddcu:[directory] is the device and directory of where the * hello service run command procedure file resides * * 2) Create a service database entry for the hello service as shown below: * * $ tcpip set service hello - * _$ /port=12345 - * _$ /protocol=tcp - * _$ /user=vms_user_account - * _$ /process_name=hello_world - * _$ /file=ddcu:[directory]hello_run.com * * 3) Enable the hello service to run as shown below: * * $ tcpip enable service hello * * * RUN INSTRUCTIONS: * * To run this example program: * * 1) Start the 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). * * 2) The auxiliary server receives the hello service request, creates a * process, then executes the commands in hello_run.com to run this * server program. This server program then logs client connection * information and client data to the service log before replying to * the client host with a message of "Hello, world!". * */ /* * INCLUDE FILES: */ #include /* define internet related constants, */ /* functions, and structures */ #include /* define network address 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 tcp/ip network constants, */ /* structures, and functions */ #include /* define unix i/o */ /* * 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, writing client connection data, * and terminating client connections. * * This example program implements a typical TCP IPv4 server using the * BSD socket API to handle network i/o operations. In addition, it * uses the auxiliary server to accept client connections. * * 1) To create a socket: * * socket() * * 2) To transfer data: * * send() * * 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; /* socket descriptor */ unsigned int client_addrlen; /* returned length of client socket */ /* address structure */ struct sockaddr_in client_addr; /* client socket address structure */ char buf[] = "Hello, world!"; /* server data buffer */ /* * init client's socket address structure */ memset( &client_addr, 0, sizeof(client_addr) ); /* * create socket */ if ( (sockfd = socket(TCPIP$C_AUXS, SOCK_STREAM, 0)) < 0 ) { perror( "Failed to create socket" ); exit( EXIT_FAILURE ); } /* * log this client connection */ client_addrlen = sizeof(client_addr); if ( getpeername(sockfd, (struct sockaddr *) &client_addr, &client_addrlen) < 0 ) { perror( "Failed to accept client connection" ); exit( EXIT_FAILURE ); } printf( "Accepted connection from host: %s, port: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port) ); /* * connection established with a client; * now attempt to write on this connection */ if ( send(sockfd, buf, sizeof(buf), 0) < 0 ) { perror( "Failed to write data to client connection" ); exit( EXIT_FAILURE ); } printf( "Data sent: %s\n", buf ); /* output server's data buffer */ /* * close socket */ if ( close(sockfd) < 0 ) { perror( "Failed to close socket" ); exit( EXIT_FAILURE ); } exit( EXIT_SUCCESS ); }