DIGITAL TCP/IP Services for OpenVMS
System Services and C Socket Programming


Previous Contents Index

A.4 TCP/IP Server Accepting a Connection from the Auxiliary Server

Example A-4 shows a TCP/IP server accepting a connection from the auxiliary server. You can access this file online in TCPIP$EXAMPLES:TCPIP$TCP_SERVER_IPC_AUXS.C.

In this example, the TCP/IP server accepts a connection from the auxiliary server, sends the string "Hello, world!" and immediately closes the connection and terminates. For this example to work correctly, you need to create a one-line command procedure and issue the SET SERVICE command after you compile and link the program, as follows:

  1. Compile and link the sample program TCPIP$EXAMPLES:TCPIP$TCP_SERVER_IPC_AUXS.C. Depending on your compiler, you can compile and link this program in any of the following ways:
  2. Create a command procedure, named HELLO_STARTUP.COM, that consists of the following line:


    $ RUN ddcu:[dir]TCPIP$TCP_SERVER_IPC_AUXS 
    

    where ddcu:[dir] is the device and directory where the file resides.

  3. Issue the following:


    TCPIP> SET SERVICE HELLO /PORT=1234 /USER=username /PROCESS=HI_WORLD - 
    _TCPIP> /FILE=ddcu:[dir]HELLO_STARTUP.COM 
     
    TCPIP>ENABLE SERVICE HELLO 
     
    

    where username is your user name and ddcu:[dir] is the device and directory where the files reside.

Example A-4 TCP/IP Server Accepting a Connection from the Auxiliary Server

#include <socket.h> 
#include <unixio.h> 
#include <string.h> 
#include <tcpip$inetdef.h> 
 
main() { 
        int s; 
        char *message = "Hello, world!\r\n"; 
 
        s = socket(TCPIP$C_AUXS, 0, 0); 
        write(s, message, strlen(message)); 
        close(s); 
} 
 

A.5 TCP/IP Client Using the IPC Socket Interface

Example A-5 shows a TCP/IP client using the C Socket interface. You can access this file online in TCPIP$EXAMPLES:TCPIP$TCP_CLIENT_IPC.C.

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.

This example is portable to UNIX. The include files are conditionally defined for both systems, and "perror" is used for error reporting.

Depending on your compiler, you can compile and link this program in any of the following ways:

Example A-5 TCP/IP Client Using the IPC Socket Interface

#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  <tcpip$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); 
 
/*--------------------------------------------------------------------*/ 
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 shut down and close socket. 
         */ 
        cleanup(shut, sock_1); 
 
 } /* end main */ 
 
/*-----------------------------------------------------------*/ 
 
void cleanup(int shut, int socket) 
 
{ 
        int    retval; 
 
        /* 
         * Shut down 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); 
 
 } 
 


Index Contents