Subversion Repositories programming

Rev

Rev 190 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * File: P1Client.java
 * Author: Ira W. Snyder (devel@irasnyder.com)
 * Class: CS380 - Computer Networking
 *
 * Assignment: Project #1
 * Date Last Modified: 2006-01-18
 *
 * Purpose: Opens a connection to a server running on port 1337 on the
 *          local machine. Sends 10 1024 Byte payloads to the server,
 *          measuring the time it takes to recieve them back.
 *          It uses this data to calculate the RTT between the applications.
 *
 ******************************************************************************/

import java.io.*;
import java.net.*;
import java.util.Date;

public class P1Client
{
    /* Instance Variables */
    private static final String exitSeq = "QUIT";
    private static final String hostName = "localhost";
    private static final int portNumber = 1337;
    private static final int numPackets = 10;
    private static final int packetSize = 1024;

    private static Date startTime;
    private static Date stopTime;

    /**
     * Method: generatePacket
     * Purpose: This method will generate a String that is size bytes in
     * length. This is assumed to be size bytes when transferred across
     * the network.
     */
    private static byte[] generatePacket (int size)
    {
        byte packet[] = new byte[size];
        int i = 0;

        for (i=0; i<size; i++)
            packet[i] = 10;

        return packet;
    }

    /**
     * Method: printTimingInfo
     * Purpose: This method will take the start and stop time and print
     * out all of the required statistics: totalTime, RTT, throughput.
     */
    private static void printTimingInfo (Date startTime, Date stopTime)
    {
        long totalTime = (stopTime.getTime() - startTime.getTime());
        double rtt = (double)totalTime / (double)numPackets;

        System.out.printf ("Time for %d x %d B packets: %d ms\n",
                numPackets, packetSize, totalTime);
        System.out.printf ("RTT per packet: %.3f ms\n", rtt);

        double _100Mbps = 100000000.0; /* bps */
        double _1MB = 8388608.0; /* bit */
        double transferTime = (rtt / 1000.0) + 1.0 / _100Mbps * _1MB;
        double throughput = _1MB / transferTime;

        System.out.printf ("Throughput: %d bits / sec\n", (int)throughput);
    }

    /**
     * Method: main
     * Purpose: This method sets up a network socket, connects to the
     * P1Server instance, and sends and times the transfer times.
     */
    public static void main (String[] args) throws IOException
    {
        Socket sendSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        /* Set up the sockets */
        try
        {
            sendSocket = new Socket (hostName, portNumber);
            out = new PrintWriter (sendSocket.getOutputStream(), true);
            in = new BufferedReader (new InputStreamReader(
                                         sendSocket.getInputStream()));
        }
        catch (UnknownHostException e)
        {
            System.err.printf ("Can't resolve hostname: %s\n", hostName);
            System.exit(1);
        }
        catch (IOException e)
        {
            System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);
            System.exit(1);
        }

        byte packet[] = generatePacket (packetSize);
        String response;
        int i = 0;

        /* Start the timer */
        startTime = new Date();

        /* Send all of the packets, recieve all of the responses */
        for (i=0; i<numPackets; i++)
        {
            out.println (packet);
            response = in.readLine();
        }

        /* Stop the timer */
        stopTime = new Date();

        /* Send the exit sequence */
        out.println (exitSeq);
        
        /* Calculate and print all timing info */
        printTimingInfo(startTime, stopTime);

        /* Cleanup */
        out.close();
        in.close();
        sendSocket.close();
    }
}