performance/tcpsclientoverhead.c

///////////////////////////////////////////////////////////////////////////////
// Filename: tcpsclientoverhead.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: measures the overhead in establishing a TCP connection
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/29 10:39:38 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


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

const int NUMBER_REPEAT = 100;                  // how often 
const int measurement_max = NUMBER_REPEAT;      // space for measurements


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



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


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

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

    if(argc!=3)
    {
        cerr << "Usage: " << argv[0] << " serveraddr port" << 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);


    int i;


    measurement mes(argv[0], "mes", measurement_max);

    for(i=0; i < NUMBER_REPEAT; i++)
    {
        mes.start();

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


        // close connection

        close(sockfd);

        mes.end();

        ssleep(100000); // sleep for 100000 mikroseconds = 100 ms = 0.1 s
                        // to allow the server to cope with all the
                        // connections
    }

    mes.writeout_histogram();
    mes.writeout_plain_histogram();

    return(EXIT_SUCCESS);
}