HP OpenVMS Systems Documentation

Content starts here

Compaq TCP/IP Services for OpenVMS
Sockets API and System Services Programming


Previous Contents Index


Appendix D
Error Codes

This appendix contains a table of Sockets API error codes and their equivalent OpenVMS system service status codes (Table D-1). The Sockets API functions use the Compaq C compiler.

Table D-1 Translation of Socket Error Codes to OpenVMS Status Codes
Sockets (Compaq C)
Error Code
OpenVMS
System Service
Status Code
Meaning
0 SS$_NORMAL Success
1 EPERM SS$_ABORT Not owner
2 ENOENT SS$_ABORT No such file or directory
3 ESRCH SS$_NOSUCHNODE No such process
4 EINTR SS$_ABORT Interrupted system call
5 EIO SS$_ABORT I/O error
6 ENXIO SS$_NOSUCHDEV No such device or address
7 E2BIG SS$_ABORT Argument list too long
8 ENOEXEC SS$_ABORT Execution format error
9 EBADF SS$_BADPARAM Bad file number
10 ECHILD SS$_ABORT No children
11 EAGAIN SS$_ABORT No more processes
12 ENOMEM SS$_INSFMEM Not enough core
13 EACCES SS$_ABORT Permission denied
14 EFAULT SS$_ACCVIO Bad address
15 ENOTBLK SS$_ABORT Block device required
16 EBUSY SS$_ABORT Mount device busy
17 EEXIST SS$_FILALRACC File exists
18 EXDEV SS$_ABORT Cross-device link
19 ENODEV SS$_ABORT No such device
20 ENOTDIR SS$_ABORT Not a directory
21 EISDIR SS$_ABORT Is a directory
22 EINVAL SS$_BADPARAM Invalid argument
23 ENFILE SS$_ABORT File table overflow
24 EMFILE SS$_ABORT Too many open files
25 ENOTTY SS$_ABORT Not a typewriter
26 ETXTBSY SS$_ABORT Text file busy
27 EFBIG SS$_ABORT File too large
28 ENOSPC SS$_ABORT No space left on device
29 ESPIPE SS$_ABORT Illegal seek
30 EROFS SS$_ABORT Read-only file system
31 EMLINK SS$_ABORT Too many links
32 EPIPE SS$_LINKDISCON Broken pipe
33 EDOM SS$_BADPARAM Argument too large
34 ERANGE SS$_TOOMUCHDATA Result too large
35 EWOULDBLOCK SS$_SUSPENDED Operation would block
36 EINPROGRESS SS$_ABORT Operation now in progress
37 EALREADY SS$_ABORT Operation already in progress
38 ENOTSOCK SS$_NOTNETDEV Socket operation on nonsocket
39 EDESTADDRREQ SS$_NOSUCHNODE Destination address required
40 EMSGSIZE SS$_TOOMUCHDATA Message too long
41 EPROTOTYPE SS$_PROTOCOL Protocol wrong type for socket
42 ENOPROTOOPT SS$_PROTOCOL Protocol not available
43 EPROTONOSUPPORT SS$_PROTOCOL Protocol not supported
44 ESOCKTNOSUPPORT SS$_PROTOCOL Socket type not supported
45 EOPNOTSUPP SS$_ILLCNTRFUNC Operation not supported on socket
46 EPFNOSUPPORT SS$_PROTOCOL Protocol family not supported
47 EAFNOSUPPORT SS$_PROTOCOL Address family not supported
48 EADDRINUSE SS$_DUPLNAM Address already in use
49 EADDRNOTAVAIL SS$_IVADDR Requested address cannot be assigned
50 ENETDOWN SS$_UNREACHABLE Network is down
51 ENETUNREACH SS$_UNREACHABLE Network is unreachable
52 ENETRESET SS$_RESET Network dropped connection on reset
53 ECONNABORTED SS$_LINKABORT Software caused connection abort
54 ECONNRESET SS$_CONNECFAIL Connection reset by peer
55 ENOBUFS SS$_INSFMEM No buffer space available
56 EISCONN SS$_FILALRACC Socket is already connected
57 ENOTCONN SS$_NOLINKS Socket is not connected
58 ESHUTDOWN SS$_SHUT Cannot send after socket shutdown
59 ETOOMANYREFS SS$_ABORT Too many references, cannot splice
60 ETIMEDOUT SS$_TIMEOUT Connection timed out
61 ECONNREFUSED SS$_REJECT Connection refused
62 ELOOP SS$_ABORT Too many levels of symbolic links
63 ENAMETOOLONG SS$_ABORT File name too long
64 EHOSTDOWN SS$_SHUT Host is down
65 EHOSTUNREACH SS$_UNREACHABLE No route to host


Appendix E
Programming Examples

Table E-1 lists the sample programs contained in this appendix and on line in the directory specified by the TCPIP$EXAMPLES system logical. See Table 1-4 and Table 1-5 for a complete list of all the sample programs provided on line in the TCPIP$EXAMPLES directory.

Table E-1 Client/Server Programming Examples
File Refer to...
TCPIP$TCP_CLIENT_SOCK.C Section E.1.1
TCPIP$TCP_SERVER_SOCK.C Section E.1.2
TCPIP$TCP_SERVER_SOCK_AUXS.C Section E.1.3
   
TCPIP$TCP_CLIENT_QIO.C Section E.2.1
TCPIP$TCP_SERVER_QIO.C Section E.2.2
TCPIP$TCP_SERVER_QIO_AUXS.C Section E.2.3
   
TCPIP$UDP_CLIENT_SOCK.C Section E.3.1
TCPIP$UDP_SERVER_SOCK.C Section E.3.2
   
TCPIP$UDP_CLIENT_QIO.C Section E.4.1
TCPIP$UDP_SERVER_QIO.C Section E.4.2

E.1 TCP Client/Server Examples (Sockets API)

This section contains examples that show the following:
  • A TCP/IP IPv4 client using BSD Version 4.x Sockets API to handle network I/O operations.
  • A TCP/IP IPv4 server using BSD Version 4.x Sockets API to handle network I/O operations.
  • A TCP/IP IPv4 server using BSD Version 4.x Sockets API to handle network I/O operations, and how the server accepts connections from the auxiliary server.

E.1.1 TCP Client

Example E-1 shows how a typical TCP IPv4 client uses the Sockets API to handle the tasks of creating a socket, initiating server connections, reading service connection data, and then terminating the server connections.

Example E-1 TCP Client (Sockets API)


#pragma module  tcpip$tcp_client_sock                   \
                "V5.1-00"
/*
 * Copyright 2000 Compaq Computer Corporation
 *
 * COMPAQ Registered in U.S. Patent and Trademark Office.
 *
 * Confidential computer software.  Valid license from  Compaq
 * or  authorized sublicensor required for possession, use  or
 * copying.  Consistent with FAR 12.211 and 12.212, Commercial
 * Computer Software,   Computer Software Documentation,   and
 * Technical Data for Commercial Items  are  licensed  to  the
 * U.S. Government under vendor's standard commercial license.
 *
 * ++
 * FACILITY:
 *
 *    EXAMPLES
 *
 * ABSTRACT:
 *
 *    This is an example of a TCP/IP IPv4 client using  4.x BSD
 *    socket  Application Programming Interface (API) to handle
 *    network I/O operations.
 *
 *    Refer to 'Build, Configuration, and Run Instructions' for
 *    details on how to build, configure, and run this program.
 *
 * ENVIRONMENT:
 *
 *    OpenVMS Alpha/VAX V7.1
 *    TCP/IP Services V5.0 or higher
 *
 * AUTHOR:
 *
 *    TCPIP Development Group,  CREATION DATE: 23-May-1989
 *
 * --
 */
        /* Build, Configuration, and Run Instructions */

/*
 * BUILD INSTRUCTIONS:
 *
 *    To build this example program use commands of the form,
 *
 *    using the DEC "C" compiler:
 *
 *      $ cc/prefix=all TCPIP$TCP_CLIENT_SOCK.C
 *      $ link TCPIP$TCP_CLIENT_SOCK
 *
 *    using the DEC "C++" compiler:
 *
 *      $ cxx/prefix=all/define=VMS TCPIP$TCP_CLIENT_SOCK.C
 *      $ link TCPIP$TCP_CLIENT_SOCK
 *
 *
 * CONFIGURATION INSTRUCTIONS:
 *
 *    No special configuration required.
 *
 *
 * RUN INSTRUCTIONS:
 *
 *    To run this example program:
 *
 *    1) Start the client's server program as shown below:
 *
 *       $ run tcpip$tcp_server_sock
 *       Waiting for a client connection on port: m
 *
 *    2) After the server program blocks, start this client program,
 *       entering the server host as shown below:
 *
 *       $ run tcpip$tcp_client_sock
 *       Enter remote host:
 *
 *       Note: You can specify a server host by using either an IPv4
 *             address in dotted-decimal notation (e.g. 16.20.10.56)
 *             or a host domain name (e.g. serverhost.compaq.com).
 *
 *    3) The client program then displays server connection information
 *       and server data as shown below:
 *
 *       Initiated connection to host: a.b.c.d, port: n
 *       Data received: Hello, world!
 *
 *    You can enter "ctrl/z" at any user prompt to terminate program
 *    execution.
 *
 */

/*
 * INCLUDE FILES:
 */

#include <in.h>                     /* define internet related constants,   */
                                    /* functions, and structures            */
#include <inet.h>                   /* define network address info          */

#include <netdb.h>                  /* define network database library info */

#include <socket.h>                 /* define BSD 4.x socket api            */
#include <stdio.h>                  /* define standard i/o functions        */
#include <stdlib.h>                 /* define standard library functions    */
#include <string.h>                 /* define string handling functions     */

#include <unixio.h>                 /* define unix i/o                      */

/*
 * NAMED CONSTANTS:
 */

#define BUFSZ           1024            /* user input buffer size           */
#define SERV_PORTNUM    12345           /* server port number               */


/*
 * FORWARD REFERENCES:
 */

int  main( void );                      /* client main                      */
void get_serv_addr( void * );           /* get server host address          */

        /* Client Main */

/*
 * FUNCTIONAL DESCRIPTION:
 *
 *    This is the client's main-line code. It handles all the tasks of the
 *    client including:  socket creation,  initiating server  connections,
 *    reading server connection data, and terminating server connections.
 *
 *    This example program implements a typical TCP IPv4 client using  the
 *    BSD socket API to handle network i/o operations as shown below:
 *
 *      1) To create a socket:
 *
 *         socket()
 *
 *      2) To initiate a connection:
 *
 *         connect()
 *
 *      3) To transfer data:
 *
 *         recv()
 *
 *      4) To shutdown a socket:
 *
 *         shutdown()
 *
 *      5) To close a socket:
 *
 *         close()
 *
 *    This function is invoked by the DCL "RUN" command (see below);  the
 *    function's  completion  status is interpreted by DCL and if needed,
 *    an error message is displayed.
 *
 * SYNOPSIS:
 *
 *    int main( void )
 *
 * FORMAL PARAMETERS:
 *
 *    ** None **
 *
 * IMPLICIT INPUTS:
 *
 *    ** None **
 *
 * IMPLICIT OUTPUTS:
 *
 *    ** None **
 *
 * FUNCTION VALUE:
 *
 *    completion status
 *
 * SIDE EFFECTS:
 *
 *    ** None **
 *
 */
int
main( void )
{
    int sockfd;                         /* connection socket descriptor     */

    char buf[512];                      /* client data buffer               */

    struct sockaddr_in serv_addr;       /* server socket address structure  */

    /*
     * init server's socket address structure
     */

    memset( &serv_addr, 0, sizeof(serv_addr) );
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port   = htons( SERV_PORTNUM );
    get_serv_addr( &serv_addr.sin_addr );

    /*
     * create connection socket
     */

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        {
        perror( "Failed to create socket" );
        exit( EXIT_FAILURE );
        }

    /*
     * connect to specified host and port number
     */

    printf( "Initiated connection to host: %s, port: %d\n",
            inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_port)
          );

    if ( connect(sockfd,
                 (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 )
        {
        perror( "Failed to connect to server" );
        exit( EXIT_FAILURE );
        }

    /*
     * connection established with a server;
     * now attempt to read on this connection
     */

    if ( recv(sockfd, buf, sizeof(buf), 0) < 0 )
        {
        perror( "Failed to read data from server connection" );
        exit( EXIT_FAILURE );
        }

    printf( "Data received: %s\n", buf );  /* output client's data buffer  */

    /*
     * shutdown connection socket (both directions)
     */
    if ( shutdown(sockfd, 2) < 0 )
        {
        perror( "Failed to shutdown server connection" );
        exit( EXIT_FAILURE );
        }

    /*
     * close connection socket
     */

    if ( close(sockfd) < 0 )
        {
        perror( "Failed to close socket" );
        exit( EXIT_FAILURE );
        }

    exit( EXIT_SUCCESS );
}

        /* Get Server Host Address */

/*
 * FUNCTIONAL DESCRIPTION:
 *
 *    This function gets the server host's address from the user and then
 *    stores it in the server's socket address structure.  Note that  the
 *    user can specify a server host by using either an IPv4  address  in
 *    dotted-decimal notation (e.g. 16.20.10.126) or a host  domain  name
 *    (e.g. serverhost.compaq.com).
 *
 *    Enter "ctrl/z" to terminate program execution.
 *
 * SYNOPSIS:
 *
 *    void get_serv_addr( void *addrptr )
 *
 * FORMAL PARAMETERS:
 *
 *    addrptr  -  pointer to socket address structure's 'sin_addr' field
 *                to store the specified network address
 *
 * IMPLICIT INPUTS:
 *
 *    ** None **
 *
 * IMPLICIT OUTPUTS:
 *
 *    ** None **
 *
 * FUNCTION VALUE:
 *
 *    ** None **
 *
 * SIDE EFFECTS:
 *
 *    Program execution is terminated if unable to read user's input
 *
 */
void
get_serv_addr( void *addrptr )
{
    char buf[BUFSZ];
    struct in_addr val;
    struct hostent *host;

    while ( TRUE )
        {
        printf( "Enter remote host: " );

        if ( fgets(buf, sizeof(buf), stdin) == NULL )
            {
            printf( "Failed to read User input\n" );
            exit( EXIT_FAILURE );
            }

        buf[strlen(buf)-1] = 0;

        val.s_addr = inet_addr( buf );

        if ( val.s_addr != INADDR_NONE )
            {
            memcpy( addrptr, &val, sizeof(struct in_addr) );
            break;
            }

        if ( (host = gethostbyname(buf)) )
            {
            memcpy( addrptr, host->h_addr, sizeof(struct in_addr) );
            break;
            }
        }
}

E.1.2 TCP Server

Example E-2 shows how a typical TCP IPv4 server uses the Sockets API to handle the tasks of creating a socket, accepting or rejecting client connections, writing client connection data, and then terminating client connections.

Example E-2 TCP Server (Sockets API)


#pragma module  tcpip$tcp_server_sock                   \
                "V5.1-00"
/*
 * Copyright 2000 Compaq Computer Corporation
 *
 * COMPAQ Registered in U.S. Patent and Trademark Office.
 *
 * Confidential computer software.  Valid license from  Compaq
 * or  authorized sublicensor required for possession, use  or
 * copying.  Consistent with FAR 12.211 and 12.212, Commercial
 * Computer Software,   Computer Software Documentation,   and
 * Technical Data for Commercial Items  are  licensed  to  the
 * U.S. Government under vendor's standard commercial license.
 *
 * ++
 * FACILITY:
 *
 *    EXAMPLES
 *
 * ABSTRACT:
 *
 *    This is an example of a TCP/IP IPv4 server using  4.x BSD
 *    socket Application Programming Interface (API) to  handle
 *    network I/O operations.
 *
 *    Refer to 'Build, Configuration, and Run Instructions' for
 *    details on how to build, configure, and run this program.
 *
 * ENVIRONMENT:
 *
 *    OpenVMS Alpha/VAX V7.1
 *    TCP/IP Services V5.0 or higher
 *
 * AUTHOR:
 *
 *    TCPIP Development Group,  CREATION DATE: 23-May-1989
 *
 * --
 */

        /* Build, Configuration, and Run Instructions */
/*
 *  BUILD INSTRUCTIONS:
 *
 *    To build this example program use commands of the form,
 *
 *    using the DEC "C" compiler:
 *
 *      $ cc/prefix=all TCPIP$TCP_SERVER_SOCK.C
 *      $ link TCPIP$TCP_SERVER_SOCK
 *
 *    using the DEC "C++" compiler:
 *
 *      $ cxx/prefix=all/define=VMS TCPIP$TCP_SERVER_SOCK.C
 *      $ link TCPIP$TCP_SERVER_SOCK
 *
 *
 * CONFIGURATION INSTRUCTIONS:
 *
 *    No special configuration required.
 *
 *
 * RUN INSTRUCTIONS:
 *
 *    To run this example program:
 *
 *    1) Start this server program as shown below:
 *
 *       $ run tcpip$tcp_server_sock
 *       Waiting for a client connection on port: m
 *
 *    2) After the server program blocks, start the client program,
 *       entering the server host as shown below:
 *
 *       $ run tcpip$tcp_client_sock
 *       Enter remote host:
 *
 *       Note: You can specify a server host by using either an IPv4
 *             address in dotted-decimal notation (e.g. 16.20.10.56)
 *             or a host domain name (e.g. serverhost.compaq.com).
 *
 *    3) The server program then displays client connection information
 *       and client data as shown below:
 *
 *       Accepted connection from host: a.b.c.d, port: n
 *       Data sent: Hello, world!
 *
 *    You can enter "ctrl/z" at any user prompt to terminate program
 *    execution.
 *
 */
/*

 * INCLUDE FILES:
 */
#include <in.h>                     /* define internet related constants,   */
                                    /* functions, and structures            */
#include <inet.h>                   /* define network address info          */

#include <netdb.h>                  /* define network database library info */

#include <socket.h>                 /* define BSD 4.x socket api            */
#include <stdio.h>                  /* define standard i/o functions        */
#include <stdlib.h>                 /* define standard library functions    */
#include <string.h>                 /* define string handling functions     */

#include <unixio.h>                 /* define unix i/o                      */

/*
 * NAMED CONSTANTS:
 */

#define SERV_BACKLOG    1               /* server backlog                   */
#define SERV_PORTNUM    12345           /* server port number               */


/*
 * FORWARD REFERENCES:
 */

int  main( void );                      /* server main                      */
        /* Server Main */

/*
 * FUNCTIONAL DESCRIPTION:
 *
 *    This is the server's main-line code. It handles all the tasks of the
 *    server including: socket creation, accepting and/or rejecting client
 *    connections, writing client connection data,  and terminating client
 *    connections.
 *
 *    This example program implements a typical TCP IPv4 server using  the
 *    BSD socket API to handle network i/o operations as shown below:
 *
 *      1) To create a socket:
 *
 *         socket()
 *
 *      2) To set REUSEADDR socket option:
 *
 *         setsockopt()
 *
 *      3) To bind internet address and port number to a socket:
 *
 *         bind()
 *
 *      4) To set an active socket to a passive (listen) socket:
 *
 *         listen()
 *
 *      5) To accept a connection request:
 *
 *         accept()
 *
 *      6) To transfer data:
 *
 *         send()
 *
 *      7) To shutdown a socket (both directions):
 *
 *         shutdown()
 *
 *      8) To close a socket:
 *
 *         close()
 *
 *    This function is invoked by the DCL "RUN" command (see below);  the
 *    function's  completion  status is interpreted by DCL and if needed,
 *    an error message is displayed.
 *
 * SYNOPSIS:
 *
 *    int main( void )
 *
 * FORMAL PARAMETERS:
 *
 *    ** None **
 *
 * IMPLICIT INPUTS:
 *
 *    ** None **
 *
 * IMPLICIT OUTPUTS:
 *
 *    ** None **
 *
 * FUNCTION VALUE:
 *
 *    completion status
 *
 * SIDE EFFECTS:
 *
 *    ** None **
 *
 */
int
main( void )
{
    int optval = 1;                     /* SO_REUSEADDR'S option value (on) */

    int conn_sockfd;                    /* connection socket descriptor     */
    int listen_sockfd;                  /* listen socket descriptor         */

    unsigned int client_addrlen;        /* returned length of client socket */
                                        /* address structure                */
    struct sockaddr_in client_addr;     /* client socket address structure  */
    struct sockaddr_in serv_addr;       /* server socket address structure  */

    char buf[] = "Hello, World!";       /* server data buffer               */

    /*
     * init client's socket address structure
     */

    memset( &client_addr, 0, sizeof(client_addr) );

    /*
     * init server's socket address structure
     */

    memset( &serv_addr, 0, sizeof(serv_addr) );
    serv_addr.sin_family      = AF_INET;
    serv_addr.sin_port        = htons( SERV_PORTNUM );
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    /*
     * create a listen socket
     */

    if ( (listen_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        {
        perror( "Failed to create socket" );
        exit( EXIT_FAILURE );
        }

    /*
     * bind server's internet address and port number to listen socket
     */

    if ( setsockopt(listen_sockfd,
                    SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0 )
        {
        perror( "Failed to set socket option" );
        exit( EXIT_FAILURE );
        }

    if ( bind(listen_sockfd,
              (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0 )
        {
        perror( "Failed to bind socket" );
        exit( EXIT_FAILURE );
        }

    /*
     * set listen socket as a passive socket
     */

    if ( listen(listen_sockfd, SERV_BACKLOG) < 0 )
        {
        perror( "Failed to set socket passive" );
        exit( EXIT_FAILURE );
        }

    /*
     * accept connection from a client
     */

    printf( "Waiting for a client connection on port: %d\n",
            ntohs(serv_addr.sin_port)
          );

    client_addrlen = sizeof(client_addr);

    conn_sockfd = accept( listen_sockfd,
                          (struct sockaddr *) &client_addr,
                          &client_addrlen
                        );
    if ( conn_sockfd < 0 )
        {
        perror( "Failed to accept client connection" );
        exit( EXIT_FAILURE );
        }

    printf( "Accepted connection from host: %s, port: %d\n",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)
          );
    /*
     * connection established with a client;
     * now attempt to write on this connection
     */

    if ( send(conn_sockfd, buf, sizeof(buf), 0) < 0 )
        {
        perror( "Failed to write data to client connection" );
        exit( EXIT_FAILURE );
        }

    printf( "Data sent: %s\n", buf );      /* output server's data buffer  */

    /*
     * shutdown connection socket (both directions)
     */

    if ( shutdown(conn_sockfd, 2) < 0 )
        {
        perror( "Failed to shutdown client connection" );
        exit( EXIT_FAILURE );
        }

    /*
     * close connection socket
     */

    if ( close(conn_sockfd) < 0 )
        {
        perror( "Failed to close socket" );
        exit( EXIT_FAILURE );
        }

    /*
     * close listen socket
     */

    if ( close(listen_sockfd) < 0 )
        {
        perror( "Failed to close socket" );
        exit( EXIT_FAILURE );
        }

    exit( EXIT_SUCCESS );
}


Previous Next Contents Index