/* ******************************************************************************* ** FILE: UDP_socket_server.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. ** ** ** ** SERVER SEQUENCE OF OPERATIONS: ** 1. Create a socket: socket() ** 2. Bind the socket to address bind() ** 3. Exchange data: recvfrom(), sendto() ** 4. Close the socket: close() ** ** ** BUILDING EXECUTABLES: ** ** 1. on VAX : ** with VAXC: ** $ CC UDP_SOCKET_SERVER.C ** $ LINK UDP_SOCKET_SERVER, 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_SERVER.C /DEFINE=TCPWARE ** $ LINK UDP_SOCKET_SERVER, SYS$INPUT/OPTIONS ** SYS$SHARE:TCPWARE_SOCKLIB_SHR/SHARE ** SYS$SHARE:VAXCRTL/SHARE ** with DECC: ** $ CC/DECC/PREFIX_LIBRARY_ENTRIES=ALL UDP_SOCKET_SERVER.C ** $ LINK UDDRIVER_SERVER ** ** 2. on ALPHA: $ CC/DECC/PREFIX_LIBRARY_ENTRIES=ALL UDP_SOCKET_SERVER.C ** $ LINK UDP_SOCKET_SERVER ** ******************************************************************************* ** REQUEST: If you have comments, please send them to "support@process.com" ******************************************************************************* */ #include #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 status; /* For return status */ int continue_IO; /* boolean continue status */ int listen_socket; int addrlen; char server_name[200]; char buffer[1024]; struct hostent *hostent_P; /* internet host address structure */ struct servent *servent_P; /* service port structure */ struct sockaddr_in server_adr; /* server socket address structure */ struct sockaddr_in client_adr; /* client socket address structure */ /* ** Setup server family and internet address. ** INADDR_ANY specifies accepting connects to any local IP address ** for this node. */ memset( &server_adr, 0, sizeof( server_adr) ); server_adr.sin_family = AF_INET; server_adr.sin_addr.s_addr = INADDR_ANY; /* ** 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)); /* Create UDP listening socket */ if ( ( listen_socket = socket( AF_INET, SOCK_DGRAM, 0) ) < 0) { perror( "socket"); exit(1); } /* Bind listening socket to server address */ addrlen = sizeof( server_adr); if ( bind( listen_socket, (struct sockaddr *)&server_adr, addrlen) < 0) { perror( "bind"); exit(1); } /* ** Keep receiving messages, till "CLOSE" is input. */ for (continue_IO = TRUE; continue_IO; ) { status = recvfrom( listen_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_adr, &addrlen); if ( status < 0) { perror( "Error reading message."); continue; } /* ** Use remote IP address to query DNS for host name. ** If unknown, just show remote IP address. */ hostent_P = gethostbyaddr( (char *) &client_adr.sin_addr,4, AF_INET); if (hostent_P) printf("\nClient data received from %s = %s\n", inet_ntoa( client_adr.sin_addr), hostent_P->h_name); else printf("\nClient data received from [%s]\n", inet_ntoa( client_adr.sin_addr) ); buffer[ status] = '\0'; /* terminate received string */ printf("Received: %s\n", buffer); /* display the message received */ if ( 0 == strcmp( buffer, "CLOSE") || 0 == strcmp( buffer, "close") ) continue_IO = FALSE; } /* ** Close the listening socket. */ status = close( listen_socket); }