United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
Compaq C

Compaq C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index


socket_fd

Returns the socket descriptor associated with a Socket Device Channel for direct use with the Compaq C RTL.

Format

#include <socket.h>

int socket_fd (int channel);


Arguments

channel

A valid Socket Device Channel.

Description

The socket_fd routine associates a valid socket channel with a Compaq C RTL file descriptor, and returns the file descriptor. The file descriptor can then be used with one of the Compaq C routines that take a file descriptor or socket descriptor as an input parameter.

Return Values

x The socket descriptor.
--1 Indicates an error; the socket descriptor cannot be allocated.

vaxc$get_sdc

Returns the Socket Device Channel (SDC) associated with a socket descriptor for direct use with the TCP/IP Services for OpenVMS product.

Format

#include <socket.h>

short int vaxc$get_sdc (int s);


Argument

s

A socket descriptor.

Description

This routine returns the SDC associated with a socket. C socket descriptors are normally used either as file descriptors or with one of the routines that takes an explicit socket descriptor as its argument. C sockets are implemented using TCP/IP Services for OpenVMS Socket Device Channels. This routine returns the SDC used by a given socket descriptor so that you can use the TCP/IP Services for OpenVMS's facilities directly by means of various I/O system services ($QIO).

Return Values

0 Indicates that s is not an open socket descriptor.
x The SDC number.

write

Writes a buffer of data to a socket or file.

Format

#include <unixio.h>

int write (int d, void *buffer, int nbytes);


Arguments

d

A descriptor that must refer to a socket or file.

buffer

The address of contiguous storage from which the output data is taken.

nbytes

The maximum number of bytes involved in the write operation.

Description

This routine attempts to write a buffer of data to a socket or file.

See also socket in this section.


Return Values

x The number of bytes written to the socket or file.
0 Indicates an error.
--1 Indicates an error; errno is set to one of the following:
  • EBADF -- The d argument is not a valid descriptor open for writing.
  • EPIPE -- An attempt was made to write to a socket that is not open for reading by any process.
  • EFAULT - Part of the array pointed to by iov or data to be written to the file points outside the process's allocated address space.
  • EWOULDBLOCK -- The nbio (nonblocking) flag is set for the socket descriptor and the process would be delayed in the write operation.
  • EINVAL -- The nbytes argument is negative.

A.9 Programming Examples

This section provides Compaq C socket communications programming examples.

Example A-1 is an example of a TCP/IP Server.

Example A-1 TCP/IP Server

/*==================================================================== 
* 
*                          Copyright (C) 1999 by 
*              Digital Equipment Corporation, Maynard, Massachusetts 
* 
* 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 Digital Equipment 
* Corporation. 
* 
* DIGITAL assumes no responsibility for the use or  reliability  of  its 
* software on equipment that is not supplied by DIGITAL. 
* 
* 
* 
*  FACILITY: 
*        INSTALL 
*      
* 
*  ABSTRACT: 
*        This is an example of a TCP/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$TCP_SERVER_IPC 
* 
*    To build this example program, use commands of the following form: 
* 
*        using the Compaq C compiler: 
* 
*        $ cc/prefix=all UCX$TCP_SERVER_IPC.C 
*        $ link UCX$TCP_SERVER_IPC 
* 
*        using the Compaq C++ compiler: 
* 
*        $ cxx/prefix=all/define=VMS UCX$TCP_SERVER_IPC.C 
*        $ link UCX$TCP_SERVER_IPC 
* 
*        using the VAX C compiler: 
* 
*        $  cc /vaxc UCX$TCP_SERVER_IPC.C 
*        $  link UCX$TCP_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 <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 
 
/* 
* Functional Description 
* 
*        This example creates a socket of type SOCK_STREAM (TCP), 
*        binds and listens on the socket, receives a message, 
*        and closes the connection.        
*        Error messages are printed to the screen. 
* 
*        IPC calls used: 
*        accept 
*        bind 
*        close 
*        gethostbyname 
*        listen 
*        recv        
*        shutdown 
*        socket        
* 
* 
* Formal Parameters 
*        The server program expects one parameter: 
*        portnumber ... port number where it will listen 
* 
* 
* Routine Value 
* 
*        Status        
*/ 
 
/*--------------------------------------------------------------------*/ 
 
cleanup( int how_many, int sock1, int sock2 ) 
{ 
 int retval; 
 
 /* 
  * Shutdown and close sock1 completely. 
  */ 
 retval = shutdown(sock1,2); 
 if (retval == -1) 
  perror ("shutdown"); 
 
        retval = close (sock1); 
 if (retval) 
  perror ("close"); 
 
 /* 
  * If given, shutdown and close sock2. 
  */ 
 if (how_many == 2) 
 { 
  retval = shutdown(sock2,2); 
  if (retval == -1) 
   perror ("shutdown"); 
 
         retval = close (sock2); 
  if (retval) 
   perror ("close"); 
 } 
 
 exit( 1 ); 
 
} /* end cleanup*/ 
 
/*--------------------------------------------------------------------*/ 
main( int argc, char **argv ) 
{ 
        int     sock_2, sock_3;                /* sockets */ 
        static  char            message[BUFSIZ]; 
static  struct  sockaddr_in     sock2_name;    /* Address struct for socket2.*/ 
static  struct  sockaddr_in     retsock2_name; /* Address struct for socket2.*/ 
        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;                        /* helpful for debugging */ 
        int     namelength; 
 
        /* 
         * Check input parameters. 
         */ 
        if (argc != 2 ) 
                { 
                printf("Usage: server portnumber.\n"); 
                exit( 1 ); 
                } 
 
        /* 
         * Open socket 2: AF_INET, SOCK_STREAM. 
         */ 
        if ((sock_2 = socket (AF_INET, SOCK_STREAM, 0)) == -1) 
                { 
                perror( "socket"); 
                exit( 1 ); 
                } 
 
        /* 
         * Get the host local name. 
         */ 
        retval = gethostname(hostname,sizeof hostname); 
        if (retval) 
                { 
                perror ("gethostname"); 
                cleanup (1, sock_2, 0); 
                } 
 
        /* 
         * Get pointer to network data structure for socket 2. 
         */ 
        if ((hostentptr = gethostbyname (hostname)) == NULL) 
                { 
                perror( "gethostbyname"); 
                cleanup(1, sock_2, 0); 
                } 
 
        /* 
         * Copy hostent data to safe storage. 
         */ 
         hostentstruct = *hostentptr; 
 
        /* 
         * Fill in the name & 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(1, sock_2, 0); 
                } 
 
        /* 
         * Listen on socket 2 for connections. 
         */ 
        retval = listen (sock_2, 5); 
        if (retval) 
                { 
                perror("listen"); 
                cleanup(1, sock_2, 0); 
                } 
 
        /* 
         * Accept connection from socket 2:      
         * accepted connection will be on socket 3. 
         */ 
        namelength = sizeof (sock2_name); 
        sock_3 = accept (sock_2, (struct sockaddr*)&sock2_name, &namelength); 
        if (sock_3 == -1) 
                { 
                perror ("accept"); 
                cleanup( 2, sock_2, sock_3); 
                } 
 
        /* 
         * Receive message from socket 1. 
         */ 
         flag = 0;      /* maybe 0 or MSG_OOB or MSG_PEEK */ 
 
        retval = recv(sock_3, message, sizeof (message), flag); 
        if (retval == -1) 
                { 
                perror ("receive"); 
                cleanup( 2, sock_2, sock_3); 
                } 
        else 
                printf (" %s\n", message); 
 
        /* 
         * Call cleanup to shutdown and close sockets. 
         */ 
        cleanup(2, sock_2, sock_3); 
 
 } /* end main */ 

Example A-2 is an example of a TCP/IP Client.

Example A-2 TCP/IP Client

/*==================================================================== 
* 
*                          Copyright (C) 1999 by 
*              Digital Equipment Corporation, Maynard, Massachusetts 
* 
* 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 Digital Equipment 
* Corporation. 
* 
* DIGITAL assumes no responsibility for the use or  reliability  of  its 
* software on equipment that is not supplied by DIGITAL. 
* 
* 
* 
*  FACILITY: 
*        INSTALL 
*      
* 
*  ABSTRACT: 
*        This is an example of a TCP/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$TCP_CLIENT_IPC 
* 
*    To build this example program, use commands of the following form: 
* 
*        using the Compaq C compiler: 
* 
*            $ cc/prefix=all UCX$TCP_CLIENT_IPC.C 
*            $ link UCX$TCP_CLIENT_IPC 
* 
*        using the Compaq C++ compiler: 
* 
*            $ cxx/prefix=all/define=VMS UCX$TCP_CLIENT_IPC.C 
*            $ link UCX$TCP_CLIENT_IPC 
* 
*        using the VAX C compiler: 
* 
*            $  cc /vaxc UCX$TCP_CLIENT_IPC.C 
*            $  link UCX$TCP_CLIENT_IPC, - 
*                    SYS$LIBRARY:UCX$IPC/LIB, - 
*                    SYS$INPUT/OPTIONS 
*            SYS$SHARE:UCX$IPC_SHR/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 
* 
*/ 
 
 
#if defined(VMS) || defined(__VMS) 
#include  <stdlib.h> 
#include  <unixio.h> 
#include  <errno.h> 
#include  <types.h> 
#include  <stdio.h> 
#include  <socket.h> 
#include  <in.h> 
#include  <netdb.h>             /* change hostent to comply with BSD 4.3*/ 
#include  <inet.h> 
#include  <ucx$inetdef.h>       /* INET symbol definitions */ 
#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 
 
/* 
* 
*   MACRO DEFINITIONS 
* 
*/ 
 
#ifndef vms 
#define TRUE 1 
#define FALSE 0 
#endif 
 
void cleanup(int shut, int socket); 
 
/* 
* Functional Description 
* 
*        This example creates a socket of type SOCK_STREAM (TCP), 
*        initiates a connection to the remote host, sends 
*        a message to the remote host, and closes the connection.        
*        Error messages are printed to the screen. 
* 
*        IPC calls used: 
*        close 
*        connect 
*        gethostbyname 
*        send 
*        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 */ 
static  char    message[] = "Hi there."; 
static  struct  sockaddr_in sock2_name;         /* Address struct for socket2.*/ 
        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;                         /* Helpful for debugging.     */ 
        int     shut = FALSE;                   /* Flag to cleanup.           */ 
 
 
        /* 
         * Check input parameters. 
         */ 
        if (argc != 3 ) 
                { 
                printf("Usage: client hostname portnumber.\n"); 
                exit(EXIT_FAILURE); 
                } 
 
 
        /* 
         * Open socket 1: AF_INET, SOCK_STREAM. 
         */ 
        if ((sock_1 = socket (AF_INET, SOCK_STREAM, 0)) == -1) 
                { 
                perror( "socket"); 
                exit(EXIT_FAILURE); 
                } 
 
 
        /* 
         *Get pointer to network data structure for socket 2 (remote host). 
         */ 
        if ((hostentptr = gethostbyname (argv[1])) == NULL) 
                { 
                perror( "gethostbyname"); 
                cleanup(shut, sock_1); 
                } 
                
 
 
        /* 
         * Copy hostent data to safe storage. 
         */ 
        hostentstruct = *hostentptr; 
 
 
        /* 
         * Fill in the name & address structure for socket 2. 
         */ 
        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); 
 
 
        /* 
         * Connect socket 1 to sock2_name. 
         */ 
        retval = connect(sock_1, (struct sockaddr *)&sock2_name, 
                        sizeof (sock2_name)); 
        if (retval) 
                { 
                perror("connect"); 
                cleanup(shut, sock_1); 
                } 
 
 
        /* 
         * Send message to socket 2. 
         */ 
        flag = 0;        /* maybe 0 or MSG_OOB */ 
        retval = send(sock_1, message ,sizeof (message), flag); 
        if (retval < 0) 
                { 
                perror ("send"); 
                shut = TRUE;        
                } 
 
 
        /* 
         * Call cleanup to shutdown and close socket. 
         */ 
        cleanup(shut, sock_1); 
 
 } /* end main */ 
 
/*-----------------------------------------------------------*/ 
void cleanup(int shut, int socket) 
        
{ 
        int        retval; 
 
        /* 
         * Shutdown socket completely -- only if it was connected. 
         */ 
        if (shut) { 
                retval = shutdown(socket,2); 
                if (retval == -1) 
                        perror ("shutdown"); 
        } 
 
 
        /* 
         * Close socket. 
         */ 
        retval = close (socket); 
        if (retval) 
                perror ("close"); 
        exit(EXIT_SUCCESS); 
 
 } /* end main */ 

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

Example A-3 UDP/IP Server

/*==================================================================== 
* 
*                          Copyright (C) 1999 by 
*             Digital Equipment Corporation, Maynard, Massachusetts 
* 
* 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 Digital Equipment 
* Corporation. 
* 
* DIGITAL assumes no responsibility for the use or  reliability  of  its 
* software on equipment that is not supplied by DIGITAL. 
* 
* 
* 
*  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 
*             Digital Equipment Corporation, Maynard, Massachusetts 
* 
* 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 Digital Equipment 
* Corporation. 
* 
* DIGITAL assumes no responsibility for the use or  reliability  of  its 
* software on equipment that is not supplied by DIGITAL. 
* 
* 
* 
*  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 */ 


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement