[an error occurred while processing this directive]

HP OpenVMS Systems

C Programming Language
Content starts here Compaq C

Compaq C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index

Example A-3 is an example of a UDP/IP Server.

Example A-3 UDP/IP Server

/*====================================================================
*
*                          Copyright (C) 2001 by
*                        Compaq Computer Corporation
*
* This software is furnished under a license and may be used and  copied
* only  in  accordance  with  the  terms  of  such  license and 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.  No title to and ownership of  the  software  is  hereby
* transferred.
*
* The information in this software is subject to change  without  notice
* and  should  not  be  construed  as  a commitment by Compaq Computer
* Corporation.
*
* Compaq assumes no responsibility for the use or  reliability  of  its
* software on equipment that is not supplied by Compaq.
*
*
*
*  FACILITY:
*        INSTALL
*
*
*  ABSTRACT:
*        This is an example of a UDP/IP server using the IPC
*        socket interface.
*
*
*  ENVIRONMENT:
*        UCX V1.2 or higher, VMS V5.2 or higher
*
*        This example is portable to ULTRIX. The include
*        files are conditionally defined for both systems, and
*        "perror" is used for error reporting.
*  BUILD INSTRUCTIONS:
*
*       To link in VAXC/VMS you must have the following
*       entries in your .opt file:
*          sys$library:ucx$ipc.olb/lib
*          sys$share:vaxcrtl.exe/share
*
*       For Compaq C or Compaq C++, compile /PREFIX=ALL and link via
*          $ link UCX$UDP_SERVER_IPC
*
*    To build this example program, use commands of the following form:
*
*        using the Compaq C compiler:
*
*            $ cc/prefix=all UCX$UDP_SERVER_IPC.C
*            $ link UCX$UDP_SERVER_IPC
*
*        using the Compaq C++ compiler:
*
*            $ cxx/prefix=all/define=VMS UCX$UDP_SERVER_IPC.C
*            $ link UCX$UDP_SERVER_IPC
*        using the VAX C compiler:
*
*            $  cc /vaxc UCX$UDP_SERVER_IPC.C
*            $  link UCX$UDP_SERVER_IPC, -
*                    SYS$LIBRARY:UCX$IPC/LIB, -
*                    SYS$INPUT/OPTIONS
*            SYS$SHARE:UCX$IPC_SHR/SHARE
*            SYS$SHARE:VAXCRTL.EXE/SHARE
*
*
*  AUTHORS:
*        UCX Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*       16 May 1996 Joseph J. Vlcek
*       Make compatible with the Compaq C and Compaq C++ compilers.
*       Add directions on how to build this example modules.
*
*/


/*
*
*  INCLUDE FILES
*
*/

#ifdef VMS
#include <descrip.h>        /* VMS descriptor stuff */
#include <errno.h>          /* Unix style error codes for IO routines. */
#include <in.h>             /* internet system Constants and structures. */
#include <inet.h>           /* Network address info. */
#include <iodef.h>          /* I/O FUNCTION CODE DEFS */
#include <lib$routines.h>   /* LIB$ RTL-routine signatures. */
#include <netdb.h>          /* Network database library info. */
#include <signal.h>         /* UNIX style Signal Value Definitions */
#include <socket.h>         /* TCP/IP socket definitions. */
#include <ssdef.h>          /* SS$_<xyz> sys ser return statistics */
#include <starlet.h>        /* Sys ser calls */
#include <stdio.h>          /* UNIX 'Standard I/O' Definitions   */
#include <stdlib.h>         /* General Utilities */
#include <string.h>         /* String handling function definitions */
#include <ucx$inetdef.h>    /* UCX network definitions */
#include <unixio.h>         /* Prototypes for UNIX emulation functions */
#else
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <time.h>               /* timeval declared here */
#endif

cleanup( int socket )
{
        int     retval;

        /*
         * Shutdown socket  completely.
         */
        retval = shutdown(socket,2);
        if (retval == -1)
                perror ("shutdown");


        /*
         * Close socket.
         */
        retval = close (socket);
        if (retval)
                perror ("close");

        exit( 1 );
} /* end cleanup */

/*
* Functional Description
*
*        This example creates a socket of type SOCK_DGRAM (UDP), binds
*        it, and selects to receive a message on the socket.
*        Error messages are printed to the screen.
*
*        IPC calls used:
*        bind
*        close
*        gethostbyname
*        recvfrom
*        select
*        shutdown
*        socket
*
*
* Formal Parameters
*        The server program expects one parameter:
*        portnumber ... port where it is listening
*
*
* Routine Value
*
*        Status
*/

/*--------------------------------------------------------------------*/
main( int argc, char **argv )
{

        int     rmask, wmask, emask;
        int     sock_2;                       /* Socket2  descriptor.       */
        int     buflen,fromlen;
        char    recvbuf[BUFSIZ];
static  struct  sockaddr_in sock1_name;       /* Address struct for socket1.*/
static  struct  sockaddr_in sock2_name;       /* Address struct for socket2.*/
        int     namelength;
        struct  hostent hostentstruct;        /* Storage for hostent data.  */
        struct  hostent *hostentptr;          /* Pointer to hostent data.   */
        static  char hostname[256];           /* Name of local host.        */
        int     retval;
        int     flag;
        struct  timeval timeout;


        /*
         * Check input parameters.
         */
        if (argc != 2 )
                {
                printf("Usage: server portnumber.\n");
                exit( 1 );
                }


        /*
         * Open socket 2: AF_INET, SOCK_DGRAM.
         */
        if ((sock_2 = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
                {
                perror( "socket");
                exit( 1 );
                }


        /*
         * Get the local host name.
         */
        retval = gethostname(hostname,sizeof hostname);
        if (retval)
                {
                perror ("gethostname");
                cleanup(sock_2);
                 }


        /*
         * Get pointer to network data structure for local host.
         */
        if ((hostentptr = gethostbyname (hostname)) == NULL)
                {
                perror( "gethostbyname");
                cleanup(sock_2);
                 }


        /*
         * Copy hostent data to safe storage.
         */
        hostentstruct = *hostentptr;


        /*
         * Fill in the address structure for socket 2.
         */
        sock2_name.sin_family = hostentstruct.h_addrtype;
        sock2_name.sin_port = htons(atoi(argv[1]));
        sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr);


        /*
         * Bind name to socket 2.
         */
        retval = bind ( sock_2,
                        (struct sockaddr*)&sock2_name,
                        sizeof sock2_name );
        if (retval)
                {
                perror("bind");
                cleanup(sock_2);
                 }


        /*
         * Select socket to receive message.
         */
        emask = wmask = 0;
        rmask = (1<<sock_2);  /* set read mask */
        timeout.tv_sec = 30;
        timeout.tv_usec = 0;

        retval = select(32, &rmask, &wmask, &emask, &timeout);
        switch (retval)
        {
          case -1:
                {
                perror("select");
                cleanup(sock_2);
                }
          case 0:
                {
                printf("Select timed out with status 0.\n");
                cleanup(sock_2);
                }
          default:
                if ((rmask & (1<<sock_2)) == 0)
                        {
                        printf("Select not reading on sock_2.\n");
                        cleanup(sock_2);
                        }
        } /*switch*/



        /*
         * Recvfrom buffer - from sock1 on sock2.
         */
        buflen = sizeof(recvbuf);
        fromlen = sizeof(sock1_name);
        flag = 0;        /* flag may be MSG_OOB and/or MSG_PEEK */

        retval = recvfrom( sock_2,
                           recvbuf,
                           buflen,
                           flag,
                           (struct sockaddr*)&sock1_name,
                           &fromlen);
        if (retval == -1)
                perror("recvfrom");
           else
                printf (" %s\n", recvbuf);

        /*
         * Call cleanup to shut down and close socket.
         */
        cleanup(sock_2);

 } /* end main */

Example A-4 is an example of a UDP/IP client.

Example A-4 UDP/IP Client

/*====================================================================
*
*                          Copyright (C) 1999 by
*                       Compaq Computer Corporation
*
* This software is furnished under a license and may be used and  copied
* only  in  accordance  with  the  terms  of  such  license and 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.  No title to and ownership of  the  software  is  hereby
* transferred.
*
* The information in this software is subject to change  without  notice
* and  should  not  be  construed  as  a commitment by Compaq Computer
* Corporation.
*
* Compaq assumes no responsibility for the use or  reliability  of  its
* software on equipment that is not supplied by Compaq.
*
*
*
*  FACILITY:
*        INSTALL
*
*
*  ABSTRACT:
*        This is an example of a UDP/IP client using the IPC
*        socket interface.
*
*
*  ENVIRONMENT:
*        UCX V1.2 or higher, VMS V5.2 or higher
*
*        This example is portable to ULTRIX. The include
*        files are conditionally defined for both systems, and
*        "perror" is used for error reporting.
*
*  BUILD INSTRUCTIONS:
*
*       To link in VAXC/VMS you must have the following
*       entries in your .opt file:
*          sys$library:ucx$ipc.olb/lib
*          sys$share:vaxcrtl.exe/share
*
*       For Compaq C or Compaq C++, compile /PREFIX=ALL and link via
*          $ link UCX$UDP_CLIENT_IPC
*
*    To build this example program, use commands of the following form:
*
*        using the Compaq C compiler:
*
*            $ cc/prefix=all UCX$UDP_CLIENT_IPC.C
*            $ link UCX$UDP_CLIENT_IPC
*
*        using the Compaq C++ compiler:
*
*            $ cxx/prefix=all/define=VMS UCX$UDP_CLIENT_IPC.C
*            $ link UCX$UDP_CLIENT_IPC
*        using the VAX C compiler:
*
*            $  cc /vaxc UCX$UDP_CLIENT_IPC.C
*            $  link UCX$UDP_CLIENT_IPC, -
*                    SYS$LIBRARY:UCX$IPC/LIB, -
*                    SYS$INPUT/OPTIONS
*            SYS$SHARE:UCX$IPC_SHR/SHARE
*            SYS$SHARE:VAXCRTL.EXE/SHARE
*
*  AUTHORS:
*        UCX Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*       16 May 1996 Joseph J. Vlcek
*       Make compatible with the Compaq C and Compaq C++ compilers.
*       Add directions on how to build this example modules.
*/


/*
*
*  INCLUDE FILES
*
*/


#ifdef VMS
#include <descrip.h>        /* VMS descriptor stuff */
#include <errno.h>          /* Unix style error codes for IO routines. */
#include <in.h>             /* internet system Constants and structures. */
#include <inet.h>           /* Network address info. */
#include <iodef.h>          /* I/O FUNCTION CODE DEFS */
#include <lib$routines.h>   /* LIB$ RTL-routine signatures. */
#include <netdb.h>          /* Network database library info. */
#include <signal.h>         /* UNIX style Signal Value Definitions */
#include <socket.h>         /* TCP/IP socket definitions. */
#include <ssdef.h>          /* SS$_<xyz> sys ser return statistics */
#include <starlet.h>        /* Sys ser calls */
#include <stdio.h>          /* UNIX 'Standard I/O' Definitions   */
#include <stdlib.h>         /* General Utilities */
#include <string.h>         /* String handling function definitions */
#include <ucx$inetdef.h>    /* UCX network definitions */
#include <unixio.h>         /* Prototypes for UNIX emulation functions */
#else
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#endif

/*-----------------------------------------------------------*/
cleanup(int socket)
{
        int     retval;

        /*
         * Shutdown socket  completely.
         */
        retval = shutdown(socket,2);
        if (retval == -1)
                perror ("shutdown");


        /*
         * Close socket.
         */
        retval = close (socket);
        if (retval)
                perror ("close");

        exit( 1 );

} /* end cleanup */

/*
* Functional Description
*
*        This example creates a socket of type SOCK_DGRAM (UDP),
*        binds it, and sends a message to the given host and port number.
*        Error messages are printed to the screen.
*
*        IPC calls used:
*        bind
*        close
*        gethostbyname
*        sendto
*        shutdown
*        socket
*
* Formal Parameters
*        The client program expects two parameters:
*        hostname ... name of remote host
*        portnumber ... port where remote host(server) is listening
*
*
* Routine Value
*
*        Status
*/


/*--------------------------------------------------------------------*/
main( int argc, char **argv )
{

        int     sock_1;                          /* Socket 1 descriptor.       */
        int     sendlen, tolen;
static  char    sendbuf[] = "Hi there.";
static struct   sockaddr_in sock2_name;          /* Address struct for socket2.*/
        int namelength;
        struct  hostent hostentstruct;          /* Storage for hostent data.  */
        struct  hostent *hostentptr;            /* Pointer to hostent data.   */
        static  char hostname[256];             /* Name of local host.        */
        int        flag;
        int        retval;


        /*
         * Check input parameters.
         */
        if (argc != 3 )
                {
                printf("Usage: client hostname portnumber.\n");
                exit( 1 );
                }

        /*
         * Open socket 1: AF_INET, SOCK_DGRAM.
         */
        if ((sock_1 = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
                 {
                perror( "socket");
                exit( 1 );
                }

        /*
         *Get pointer to network data structure for given host.
         */
        if ((hostentptr = gethostbyname (argv[1])) == NULL)
                {
                perror( "gethostbyname");
                cleanup(sock_1);
                }


        /*
         * Copy hostent data to safe storage.
         */
        hostentstruct = *hostentptr;


        /*
         * Fill in the address structure for socket 2 (to receive message).
         */
        sock2_name.sin_family = hostentstruct.h_addrtype;
        sock2_name.sin_port = htons(atoi(argv[2]));
        sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr);


        /*
         * Initialize send block.
         */
        sendlen = sizeof sendbuf;
        tolen = sizeof sock2_name;
        flag = 0;                /* flag may be MSG_OOB */


        /*
         * Send message from socket 1 to socket 2.
         */
        retval = sendto( sock_1, sendbuf, sendlen, flag,
                         (struct sockaddr*)&sock2_name, tolen);
        if (retval == -1)
                {
                perror ( "sendto");
                cleanup(sock_1);
                }

        /*
         * Call cleanup to shut down and close socket.
         */
        cleanup(sock_1);

 } /* end main */


Appendix B
Version-Dependency Tables

New functions are added to the Compaq C Run-Time Library with each version of Compaq C. These functions are implemented and shipped with the OpenVMS operating system, while the documentation and header files containing their prototypes are shipped with versions of the Compaq C compiler.

You might have a newer version of Compaq C that has header files and documentation for functions that are not supported on your older OpenVMS system. For example, if your target operating system platform is OpenVMS Version 6.2, you cannot use Compaq C RTL functions that were introduced on OpenVMS Version 7.0, even though they are documented in this manual.

This appendix contains several tables that list what Compaq C RTL functions are supported on recent OpenVMS versions. This is helpful for determining the functions to avoid using on your target OpenVMS platforms.

Also, for Compaq C and C++ Version 5.6 and higher, a C RTL backport object library is included with the compiler distribution kit. The backport object library allows developers on older versions of OpenVMS to use the latest C run-time library functions. For more information, see the file SYS$LIBRARY:DECC$CRTL.README on your system.

B.1 Functions Available on all OpenVMS VAX and OpenVMS Alpha Versions

Table B-1 lists functions available on all OpenVMS VAX and OpenVMS Alpha versions.

Table B-1 Functions Available on All OpenVMS Systems
abort abs access acos
alarm asctime asin assert
atan2 atan atexit atof
atoi atoll (Alpha) atol atoq (Alpha)
box brk bsearch cabs
calloc ceil cfree chdir
chmod chown clearerr clock
close cosh cos creat
ctermid ctime cuserid decc$crtl_init
decc$fix_time decc$from_vms decc$match_wild decc$record_read
decc$record_write decc$set_reentrancy decc$to_vms decc$translate_vms
delete delwin difftime div
dup2 dup ecvt endwin
execle execlp execl execve
execvp execv exit _exit
exp fabs fclose fcvt
fdopen feof ferror fflush
fgetc fgetname fgetpos fgets
fileno floor fmod fopen
fprintf fputc fputs fread
free freopen frexp fscanf
fseek fsetpos fstat fsync
ftell ftime fwait fwrite
gcvt getchar getcwd getc
getegid getenv geteuid getgid
getname getpid getppid gets
getuid getw gmtime gsignal
hypot initscr isalnum isalpha
isapipe isascii isatty iscntrl
isdigit isgraph islower isprint
ispunct isspace isupper isxdigit
kill labs ldexp ldiv
llabs (Alpha) lldiv (Alpha) localeconv localtime
log10 log longjmp longname
lseek lwait malloc mblen
mbstowcs mbtowc memchr memcmp
memcpy memmove memset mkdir
mktemp mktime modf mvwin
mv[w]addstr newwin nice open
overlay overwrite pause perror
pipe pow printf putchar
putc puts putw qabs (Alpha)
qdiv (Alpha) qsort raise rand
read realloc remove rename
rewind sbrk scanf scroll
setbuf setgid setjmp setlocale
setuid setvbuf sigblock signal
sigpause sigstack (VAX) sigvec sinh
sin sleep sprintf sqrt
srand sscanf ssignal stat
strcat strchr strcmp strcoll
strcpy strcspn strerror strftime
strlen strncat strncmp strncpy
strpbrk strrchr strspn strstr
strtod strtok strtoll (Alpha) strtol
strtoq (Alpha) strtoull (Alpha) strtoul strtouq (Alpha)
strxfrm subwin system tanh
tan times time tmpfile
tmpnam toascii tolower _tolower
touchwin toupper _toupper ttyname
umask ungetc vaxc$calloc_opt vaxc$cfree_opt
vaxc$crtl_init vaxc$establish vaxc$free_opt vaxc$malloc_opt
vaxc$realloc_opt va_arg va_count va_end
va_start va_start_1 vfork vfprintf
vprintf vsprintf wait wcstombs
wctomb write [w]addch [w]addstr
[w]clear [w]clrattr [w]clrtobot [w]clrtoeol
[w]delch [w]deleteln [w]erase [w]getch
[w]getstr [w]inch [w]insch [w]insertln
[w]insstr [w]move [w]printw [w]refresh
[w]scanw [w]setattr [w]standend [w]standout


Previous Next Contents Index