z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


The client sample program in C

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

The following C socket server program is the MTCCLNT member in the SEZAINST data set.


/*** IBMCOPYR ********************************************************/
/*                                                                   */
/* Component Name: MTCCLNT                                           */
/*                                                                   */
/*                                                                   */
/* Copyright:    Licensed Materials - Property of IBM                */
/*                                                                   */
/*               "Restricted Materials of IBM"                       */
/*                                                                   */
/*               5647-A01                                            */
/*                                                                   */
/*               (C) Copyright IBM Corp. 1977, 1998                  */
/*                                                                   */
/*               US Government Users Restricted Rights -             */
/*               Use, duplication or disclosure restricted by        */
/*               GSA ADP Schedule Contract with IBM Corp.            */
/*                                                                   */
/* Status:       CSV2R6                                              */
/*                                                                   */
/*  SMP/E Distribution Name: EZAEC047                                */
/*                                                                   */
/*                                                                   */
/*** IBMCOPYR ********************************************************/

/*********************************************************************/
/* C Socket Client Program                                           */
/*                                                                   */
/* This code sends and receives mgs with the server subtask.         */
/*********************************************************************/

static char ibmcopyr[] =
   "MTCCLNT - Licensed Materials - Property of IBM. "
   "This module is \"Restricted Materials of IBM\" "
   "5647-A01 (C) Copyright IBM Corp. 1994, 1996. "
   "See IBM Copyright Instructions.";

#include <manifest.h>
#include <bsdtypes.h>
#include <in.h>
#include <netdb.h>
#include <socket.h>
#include <inet.h>
#include <errno.h>
#include <tcperrno.h>
#include <bsdtime.h>
#include <stdio.h>

int dosend(int *s);
int dorecv(int *s);
int doconn(int *s, unsigned long *octaddrp, unsigned short port);
void getsock(int *s);


/*
 * Client
 */
main(int argc, char **argv)
{
    int gotbytes;              /* number of bytes received            */
    int sndbytes;              /* number of bytes sent                */
    int s;                     /* socket descriptor                   */
    int rc;                    /* return code                         */
    struct in_addr octaddr;    /* host internet address (binary)      */
    unsigned short port;       /* port number sent as parameter       */
    char * charaddr;           /* host internet address (dotted dec)  */
    struct hostent *hostnm;    /* server host name information        */

    /*
     * Check Arguments Passed. Should be hostname and port.
     */
    if (argc != 3) {
        fprintf(stderr, "Usage: %s hostname port\n", argv[0]);
        exit(1);
    }
    /*
     * The host name is the first argument. Get the server address.
     */
    hostnm = gethostbyname(argv[1]);
    if (hostnm == (struct hostent *) 0) {
        fprintf(stderr, "Gethostbyname failed\n");
        exit(2);
    }
    octaddr.s_addr = *((unsigned long *)hostnm->h_addr);

    /*
     * The port is the second argument.
     */
    port = (unsigned short) atoi(argv[2]);
    fprintf(stdout, "Clnt: port = %d\n", port);

    getsock(&s);
    printf("Clnt: our socket is %d\n", s);

    charaddr = inet_ntoa(octaddr);
    printf("Clnt: address of host is %8s\n", charaddr);

    rc = doconn(&s, &octaddr.s_addr, port);
    if (rc < 0)
        tcperror("Clnt: error for connect");
    else {
        printf("Clnt: rc from connect is %d\n", rc);
        do {
            gotbytes = dorecv(&s);
            sndbytes = dosend(&s);
        } while (0);
     /* } while (sndbytes > 0); do simplified situation first ***/
        sleep(15);
    }
}

/*-----------------*/
/*    getsock()    */
/*-----------------*/
void getsock(int *s)
{
    int temp;
    temp = socket(AF_INET, SOCK_STREAM, 0);
    *s = temp;
    return;
}

/*-----------------*/
/*    doconn()     */
/*-----------------*/
int doconn(int *s, unsigned long *octaddrp, unsigned short port)
{
    int rc;
    int temps;
    struct sockaddr_in tsock;

    memset(&tsock, 0, sizeof(tsock));
    tsock.sin_family      = AF_INET;
    tsock.sin_port        = htons(port);
    tsock.sin_addr.s_addr = *octaddrp;

    temps = *s;
    rc = connect(temps, (struct sockaddr *)&tsock, sizeof(tsock));
    return rc;
}

/*-----------------*/
/*    dorecv()     */
/*-----------------*/
int dorecv(int *s)
{
    int temps;
    int gotbytes;
    char data[100];

    temps = *s;

    gotbytes = recv(temps, data, sizeof(data), 0);
    if (gotbytes < 0) {
        tcperror("Clnt: error for recv");
    }
    else
        printf("Clnt: data recv: %s\n", data);
    return gotbytes;
}

/*-----------------*/
/*    dosend()     */
/*-----------------*/
int dosend(int *s)
{
    int temps;
    int sndbytes;
    char data[50];

    temps = *s;
    gets(data);
    printf("clnt: data to send: %s\n", data);
    sndbytes = send(temps, data, sizeof(data), 0);
    if (sndbytes < 0) {
        tcperror("Clnt: error for send");
    }
    else
        printf("Clnt: sent %d bytes to server subtask\n", sndbytes);
    return sndbytes;
}
Figure 1. MTCCLNT C socket server program sample

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014