Subversion Repositories programming

Rev

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

Rev Author Line No. Line
186 ira 1
/*******************************************************************************
2
 * File: P1Client.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * Class: CS380 - Computer Networking
5
 *
6
 * Assignment: Project #1
7
 * Date Last Modified: 2006-01-18
8
 *
9
 * Purpose: Opens a connection to a server running on port 1337 on the
10
 *          local machine. Sends 10 1024 Byte payloads to the server,
11
 *          measuring the time it takes to recieve them back.
12
 *          It uses this data to calculate the RTT between the applications.
13
 *
14
 ******************************************************************************/
15
 
16
import java.io.*;
17
import java.net.*;
188 ira 18
import java.util.Date;
186 ira 19
 
20
public class P1Client
21
{
189 ira 22
    /* Instance Variables */
23
    private static final String exitSeq = "QUIT";
24
    private static final String hostName = "localhost";
25
    private static final int portNumber = 1337;
26
    private static final int numPackets = 10;
27
    private static final int packetSize = 1024;
186 ira 28
 
188 ira 29
    private static Date startTime;
30
    private static Date stopTime;
189 ira 31
 
32
    /**
33
     * Method: generatePacket
34
     * Purpose: This method will generate a String that is size bytes in
35
     * length. This is assumed to be size bytes when transferred across
36
     * the network.
37
     */
191 ira 38
    private static byte[] generatePacket (int size)
189 ira 39
    {
191 ira 40
        byte packet[] = new byte[size];
189 ira 41
        int i = 0;
42
 
43
        for (i=0; i<size; i++)
190 ira 44
            packet[i] = 10;
189 ira 45
 
46
        return packet;
47
    }
48
 
49
    /**
50
     * Method: printTimingInfo
51
     * Purpose: This method will take the start and stop time and print
52
     * out all of the required statistics: totalTime, RTT, throughput.
53
     */
54
    private static void printTimingInfo (Date startTime, Date stopTime)
55
    {
56
        long totalTime = (stopTime.getTime() - startTime.getTime());
57
        double rtt = (double)totalTime / (double)numPackets;
58
 
59
        System.out.printf ("Time for %d x %d B packets: %d ms\n",
60
                numPackets, packetSize, totalTime);
61
        System.out.printf ("RTT per packet: %.3f ms\n", rtt);
62
 
63
        double _100Mbps = 100000000.0; /* bps */
64
        double _1MB = 8388608.0; /* bit */
65
        double transferTime = (rtt / 1000.0) + 1.0 / _100Mbps * _1MB;
66
        double throughput = _1MB / transferTime;
67
 
68
        System.out.printf ("Throughput: %d bits / sec\n", (int)throughput);
69
    }
70
 
71
    /**
72
     * Method: main
73
     * Purpose: This method sets up a network socket, connects to the
74
     * P1Server instance, and sends and times the transfer times.
75
     */
186 ira 76
    public static void main (String[] args) throws IOException
77
    {
188 ira 78
        Socket sendSocket = null;
79
        PrintWriter out = null;
80
        BufferedReader in = null;
81
 
189 ira 82
        /* Set up the sockets */
188 ira 83
        try
84
        {
85
            sendSocket = new Socket (hostName, portNumber);
86
            out = new PrintWriter (sendSocket.getOutputStream(), true);
87
            in = new BufferedReader (new InputStreamReader(
88
                                         sendSocket.getInputStream()));
89
        }
90
        catch (UnknownHostException e)
91
        {
189 ira 92
            System.err.printf ("Can't resolve hostname: %s\n", hostName);
188 ira 93
            System.exit(1);
94
        }
95
        catch (IOException e)
96
        {
189 ira 97
            System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);
188 ira 98
            System.exit(1);
99
        }
100
 
191 ira 101
        byte packet[] = generatePacket (packetSize);
189 ira 102
        String response;
188 ira 103
        int i = 0;
104
 
105
        /* Start the timer */
106
        startTime = new Date();
189 ira 107
 
108
        /* Send all of the packets, recieve all of the responses */
188 ira 109
        for (i=0; i<numPackets; i++)
110
        {
111
            out.println (packet);
112
            response = in.readLine();
113
        }
114
 
115
        /* Stop the timer */
116
        stopTime = new Date();
117
 
190 ira 118
        /* Send the exit sequence */
119
        out.println (exitSeq);
120
 
188 ira 121
        /* Calculate and print all timing info */
122
        printTimingInfo(startTime, stopTime);
123
 
124
        /* Cleanup */
125
        out.close();
126
        in.close();
127
        sendSocket.close();
186 ira 128
    }
129
}
130