distSOCKET/tcpclient.c

///////////////////////////////////////////////////////////////////////////////
// Filename: tcpclient.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: show how to use TCP (this file: TCP client) (socket version)
//          needs tcpserver(.c) as TCP server
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/20 09:36:18 muellerg: created
// 96/02/21 05:40:07 muellerg: documentation cleanup
// 96/03/19 05:22:00 muellerg: functionality added
//
// possible improvements:
// 
// more error checking
// should accept also ascii names for computer names
// port number check
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#ifndef sun
#include <strings.h>
#endif
#include <unistd.h>



// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*
 * send data to tcp server and get result
 *
 * in: data:    data to send
 *     sockfd:  fd to use
 */

void do_it(char *data, int sockfd)
{
    int n;
    char *buffer = new char[strlen(data)+1];

    if (writen(sockfd, data, strlen(data)+1) != (int)(strlen(data)+1) )
        error.system("TCP client: write error");

    // force TCP to send data (it does not if \n is not sent)

    if (writen(sockfd, "\n", 1) != 1 )
        error.system("TCP client: write error");
        
    // get result

    n = readn(sockfd, buffer, strlen(data)+1);
    if (n < 0)
        error.system("TCP client: read error");

    if(strcmp(data,buffer) != 0)
    {
        cout << "TCP client: returned result is not equal to sent string!"
             << endl;
    }
    else
        cout << "TCP client: returned string is equal to sent string" << endl;

    delete[] buffer;
}




// Main /////////////////////////////////////////////////////////////// Main //


/*
 * Example of client using the TCP protocol.
 *
 * paramteters:
 *
 *   argv[1]: server internet address (in dot-form)
 *   argv[2]: port number of server
 *   argv[3]: string to sent to server  
 */

int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    if(argc!=4)
    {
        cerr << "Usage: " << argv[0] << " serveraddr port string" << endl;
        exit(EXIT_FAILURE);
    }

    int sockfd;
    struct sockaddr_in  serv_addr;
    int portnumber=-1;


    // get port number

    portnumber = atoi(argv[2]);
    if(portnumber <1)
    {
        cerr << "illegal port number" << endl;
        exit(EXIT_FAILURE);
    }

    // Fill in the structure "serv_addr" with the address of the
    // server that we want to connect with.

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family      = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]); // addr of host
    serv_addr.sin_port        = htons(portnumber);


    // create a TCP socket (an Internet stream socket).

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        error.system("client: can't create stream socket");


    // connect to the server

    if (connect(sockfd, (struct sockaddr *) &serv_addr,
                            sizeof(serv_addr)) < 0)
        error.system("client: can't connect to server");

    do_it(argv[3], sockfd);

    close(sockfd);

    return(EXIT_SUCCESS);
}