/* ******************************************************************************* ** FILE: UDP_socket_client.C -- example program demonstrating usage of ** network programming library using UNIX-like connectionless UDP sockets. ** ** PRODUCT: TCPware for VMS ** ** VERSION: V5.6 ** ** Copyright (c) 2001, 2002 by ** Process Software LLC ** Framingham, Massachusetts ** ** Copyright (c) 1997-1999 by ** Process Software Corporation ** Framingham, Massachusetts ** ** This software is furnished under a license for use on a ** single computer system and may be copied only with the ** inclusion of the above copyright notice. This software, or ** any other copies thereof, may not be provided or otherwise ** made available to any other person except for use on such ** system and to one who agrees to these license terms. Title ** to and ownership of the software shall at all times remain ** in Process Software LLC's name. ** ** The information in this document is subject to change ** without notice and should not be construed as a commitment ** by Process Software LLC. Process Software LLC assumes no ** responsibility for any errors that may appear in this document. ** ** ** ABSTRACT: ** ** UDP_socket_server.c and UDP_socket_client.c are a pair of example programs ** illustrating the use of datagrams via UNIX-like connectionless UDP sockets. ** ** ** ** CLIENT SEQUENCE OF OPERATIONS: ** 1. Create a socket: socket() ** 2. Exchange data: recvfrom(), sendto() ** 3. Close the socket: close() ** ** ** BUILDING EXECUTABLES: ** ** 1. on VAX : ** with VAXC: ** $ CC UDP_SOCKET_CLIENT.C ** $ LINK UDP_SOCKET_CLIENT, TCPWARE:UCX$IPC/LIB, - ** SYS$INPUT/OPTIONS ** SYS$SHARE:VAXCRTL/SHARE ** ** Instead of UCX's BG devices, TCpware's socket library can be used. ** ("/DEFINE=TCPWARE" allows close() to be redefined to a socket function.) : ** ** $ CC UDP_SOCKET_CLIENT.C /DEFINE=TCPWARE ** $ LINK UDP_SOCKET_CLIENT, SYS$INPUT/OPTIONS ** SYS$SHARE:TCPWARE_SOCKLIB_SHR/SHARE ** SYS$SHARE:VAXCRTL/SHARE ** with DECC: ** $ CC/DECC/PREFIX_LIBRARY_ENTRIES=ALL UDP_SOCKET_CLIENT.C ** $ LINK UDDRIVER_CLIENT ** ** 2. on ALPHA: $ CC/DECC/PREFIX_LIBRARY_ENTRIES=ALL UDP_SOCKET_CLIENT.C ** $ LINK UDP_SOCKET_CLIENT ** ******************************************************************************* ** REQUEST: If you have comments, please send them to "support@process.com" ******************************************************************************* */ #include #include #include #include #include #include #include #include #define SERVER_PORT 65000 /* if getservbyname fails, use this port number */ #define SERVICE "test_discard" #define PROTO "udp" #ifdef VAXC #ifdef TCPWARE # define close socket_close #endif /* TCPWARE */ /* function prototypes */ int socket_close( int s); #endif /* VAXC */ main() { int client_socket; int addrlen; int status; /* For return status */ int continue_IO; /* boolean continue flag */ int got_host; char server_name[200]; int buflen; char buffer[1024]; struct servent *servent_P; struct hostent *hostent_P; struct sockaddr_in server_adr; /* ** Get server port number for named service. ** If unknown ( == 0 or = -1), use default define. */ memset( &server_adr, 0, sizeof( server_adr) ); if ( (int)(servent_P = getservbyname( SERVICE, PROTO)) > 0) server_adr.sin_port = servent_P->s_port; else server_adr.sin_port = htons(SERVER_PORT); printf("Will receive on server port #: %d\n\n", ntohs(server_adr.sin_port)); /* Query user for server name and get corresponding internet address. */ for (got_host = FALSE; got_host == FALSE; ) { printf("Enter name of remote host: "); gets( server_name); if ((hostent_P = gethostbyname(server_name)) == NULL) { printf("Error, gethostbyname failed\n"); } else { got_host = TRUE; server_adr.sin_family = hostent_P->h_addrtype; server_adr.sin_addr = *( (struct in_addr *)(hostent_P->h_addr) ); } } /* Create socket */ if ( ( client_socket = socket( AF_INET, SOCK_DGRAM, 0) ) < 0) { perror( "client_socket"); exit(1); } /* ** Keep getting input string & send to the server, till "CLOSE" is input. */ printf("\n\nKeep getting keyboard input & sending to the server,\n till ^Z (client exit) or 'CLOSE' (server exit) is input.\n\n"); for (continue_IO = TRUE; continue_IO; ) { /* ** Prepare message to send: ** Setup message (Replace with your application function.) */ printf("\nInput string to send. sends default: \n"); if ( gets( buffer) == NULL) break; buflen = strlen(buffer); /* if no input, setup default message */ if ( 0 == buflen) { strcpy( buffer, "User Datagram Protocol is described in RFC 768"); buflen = strlen(buffer); } /* ** Send the message */ addrlen = sizeof( server_adr); status = sendto( client_socket, buffer, buflen, 0, (struct sockaddr *)&server_adr, addrlen); if ( status < 0 ) { perror("Error sending message."); break; } if ( 0 == strcmp( buffer, "CLOSE") || 0 == strcmp( buffer, "close") ) continue_IO = FALSE; } /* Close the socket */ close( client_socket); }