Subversion Repositories programming

Rev

Rev 186 | Rev 189 | Go to most recent revision | 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
{

    public static final String exitSeq = "QUIT";
    public static final String hostName = "localhost";
    public static final int portNumber = 1337;
    public static final int numPackets = 200000;
    public static final int packetSize = 1024;

    private static Date startTime;
    private static Date stopTime;
    
    public static void main (String[] args) throws IOException
    {
        Socket sendSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

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

        String packet = generatePacket (packetSize);
        String response = new String();
        int i = 0;

        /* Start the timer */
        startTime = new Date();
        
        for (i=0; i<numPackets; i++)
        {
            out.println (packet);
            response = in.readLine();
        }

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

        /* Calculate and print all timing info */
        printTimingInfo(startTime, stopTime);

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

    public static String generatePacket (int size)
    {
        String packet = new String();
        int i = 0;

        for (i=0; i<size; i++)
            packet += "Z";

        return packet;
    }

    public static void printTimingInfo (Date startTime, Date stopTime)
    {
        long totalTime = (stopTime.getTime() - startTime.getTime());
        double rtt = (double)totalTime / (double)numPackets;
        double totalBytes = (double)packetSize * (double)numPackets;
        double bw  = totalBytes / (double)totalTime * (double)1000.0 /* ms */;

        System.out.println ("Time for " + numPackets + " packets (millisec): "
                            + totalTime);

        System.out.println ("RTT per packet: " + rtt);
        System.out.println ("Total Bandwidth: " + bw + " Bytes / sec");
    }
}