A.9 Programming Examples

This section provides DEC C socket communications programming examples.

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

Example A-1 TCP/IP Server

/*====================================================================
*
*                          Copyright (C) 1989 by
*              Digital Equipment Corporation, Maynard, Mass.
*
* 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.

*
*        To link in DEC C on OpenVMS Systems, you must have the following
*        entries in your .opt file:
*           sys$library:ucx$ipc.olb/lib
*           sys$share:vaxcrtl.exe/share

*
*  AUTHORS:
*        UCX Developer
*

*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*/


/*
*
*  INCLUDE FILES
*
*/


#ifdef __VMS
#include  <errno.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


/*
* 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
*/


/*--------------------------------------------------------------------*/
main(argc,argv)
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();
                }


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


        /*
         * 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, &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, &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 */


/*-----------------------------------------------------------*/

cleanup(how_many, sock1, sock2)
int        how_many;
int        sock1, 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();

} /* end cleanup*/

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

Example A-2 TCP/IP Client

/*====================================================================
*
*                          Copyright (C) 1989 by
*              Digital Equipment Corporation, Maynard, Mass.
*
* 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.

*
*        To link in DEC C on OpenVMS Systems, you must have the following
*        entries in your .opt file:
*           sys$library:ucx$ipc.olb/lib
*           sys$share:vaxcrtl.exe/share

*
*  AUTHORS:
*        UCX Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*/


/*
*
*  INCLUDE FILES
*
*/


#ifdef __VMS
#include  <errno.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


/*
* 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(argc,argv)
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();
                }


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


        /*
         *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, &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 */


/*-----------------------------------------------------------*/

cleanup(shut, socket)
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();

 } /* end main */

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

Example A-3 UDP/IP Server

/*====================================================================
*
*                          Copyright (C) 1989 by
*              Digital Equipment Corporation, Maynard, Mass.
*
* 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.

*
*        To link in DEC C on OpenVMS Systems, you must have the following
*        entries in your .opt file:
*           sys$library:ucx$ipc.olb/lib
*           sys$share:vaxcrtl.exe/share

*
*  AUTHORS:
*        UCX Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*/


/*
*
*  INCLUDE FILES
*
*/


#ifdef __VMS
#include  <errno.h>
#include  <stdio.h>
#include  <socket.h>               /* timeval declared here. */
#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>
#include <time.h>                 /* timeval declared here. */
#endif


/*
* 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(argc, argv)
int        argc;
char       **argv;
{


        unsigned long 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();
                }


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


        /*
         * 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, &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, &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 */


/*-----------------------------------------------------------*/

cleanup(socket)
int  socket;

{
        int   retval;

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


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

        exit();

} /* end cleanup */

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

Example A-4 UDP/IP Client

/*====================================================================
*
*                          Copyright (C) 1989 by
*              Digital Equipment Corporation, Maynard, Mass.
*
* 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.

*
*        To link in DEC C on OpenVMS Systems, you must have the following
*        entries in your .opt file:
*           sys$library:ucx$ipc.olb/lib
*           sys$share:vaxcrtl.exe/share

*
*  AUTHORS:
*        UCX Developer
*
*  CREATION DATE: May 23, 1989
*
*  MODIFICATION HISTORY:
*
*/



/*
*
*  INCLUDE FILES
*
*/

#ifdef __VMS
#include  <errno.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



/*
* 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(argc, argv)
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();
                }

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

        /*
         *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, &sock2_name,tolen);
        if (retval == -1)
                {
                perror ( "sendto");
                cleanup(sock_1);
                }

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

 } /* end main */

/*-----------------------------------------------------------*/

cleanup(socket)
int        socket;

{
        int        retval;

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

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

        exit();

} /* end cleanup */


Previous Page | Next Page | Table of Contents | Index