Subversion Repositories programming

Rev

Rev 188 | Rev 190 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 188 Rev 189
Line 17... Line 17...
17
import java.net.*;
17
import java.net.*;
18
import java.util.Date;
18
import java.util.Date;
19
 
19
 
20
public class P1Client
20
public class P1Client
21
{
21
{
22
 
-
 
-
 
22
    /* Instance Variables */
23
    public static final String exitSeq = "QUIT";
23
    private static final String exitSeq = "QUIT";
24
    public static final String hostName = "localhost";
24
    private static final String hostName = "localhost";
25
    public static final int portNumber = 1337;
25
    private static final int portNumber = 1337;
26
    public static final int numPackets = 200000;
26
    private static final int numPackets = 10;
27
    public static final int packetSize = 1024;
27
    private static final int packetSize = 1024;
28
 
28
 
29
    private static Date startTime;
29
    private static Date startTime;
30
    private static Date stopTime;
30
    private static Date stopTime;
-
 
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
     */
-
 
38
    private static String generatePacket (int size)
-
 
39
    {
-
 
40
        String packet = new String();
-
 
41
        int i = 0;
-
 
42
 
-
 
43
        for (i=0; i<size; i++)
-
 
44
            packet += "Z";
-
 
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);
31
    
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
     */
32
    public static void main (String[] args) throws IOException
76
    public static void main (String[] args) throws IOException
33
    {
77
    {
34
        Socket sendSocket = null;
78
        Socket sendSocket = null;
35
        PrintWriter out = null;
79
        PrintWriter out = null;
36
        BufferedReader in = null;
80
        BufferedReader in = null;
37
 
81
 
-
 
82
        /* Set up the sockets */
38
        try
83
        try
39
        {
84
        {
40
            sendSocket = new Socket (hostName, portNumber);
85
            sendSocket = new Socket (hostName, portNumber);
41
            out = new PrintWriter (sendSocket.getOutputStream(), true);
86
            out = new PrintWriter (sendSocket.getOutputStream(), true);
42
            in = new BufferedReader (new InputStreamReader(
87
            in = new BufferedReader (new InputStreamReader(
43
                                         sendSocket.getInputStream()));
88
                                         sendSocket.getInputStream()));
44
        }
89
        }
45
        catch (UnknownHostException e)
90
        catch (UnknownHostException e)
46
        {
91
        {
47
            System.err.println ("Can't resolve hostname: " + hostName);
92
            System.err.printf ("Can't resolve hostname: %s\n", hostName);
48
            System.exit(1);
93
            System.exit(1);
49
        }
94
        }
50
        catch (IOException e)
95
        catch (IOException e)
51
        {
96
        {
52
            System.err.println ("Couldn't get I/O for " +
97
            System.err.printf ("Couldn't get I/O for the connection to: %s\n", hostName);
53
                                "the connection to: " + hostName);
-
 
54
            System.exit(1);
98
            System.exit(1);
55
        }
99
        }
56
 
100
 
57
        String packet = generatePacket (packetSize);
101
        String packet = generatePacket (packetSize);
58
        String response = new String();
102
        String response;
59
        int i = 0;
103
        int i = 0;
60
 
104
 
61
        /* Start the timer */
105
        /* Start the timer */
62
        startTime = new Date();
106
        startTime = new Date();
63
        
107
 
-
 
108
        /* Send all of the packets, recieve all of the responses */
64
        for (i=0; i<numPackets; i++)
109
        for (i=0; i<numPackets; i++)
65
        {
110
        {
66
            out.println (packet);
111
            out.println (packet);
67
            response = in.readLine();
112
            response = in.readLine();
68
        }
113
        }
Line 76... Line 121...
76
        /* Cleanup */
121
        /* Cleanup */
77
        out.close();
122
        out.close();
78
        in.close();
123
        in.close();
79
        sendSocket.close();
124
        sendSocket.close();
80
    }
125
    }
81
 
-
 
82
    public static String generatePacket (int size)
-
 
83
    {
-
 
84
        String packet = new String();
-
 
85
        int i = 0;
-
 
86
 
-
 
87
        for (i=0; i<size; i++)
-
 
88
            packet += "Z";
-
 
89
 
-
 
90
        return packet;
-
 
91
    }
-
 
92
 
-
 
93
    public static void printTimingInfo (Date startTime, Date stopTime)
-
 
94
    {
-
 
95
        long totalTime = (stopTime.getTime() - startTime.getTime());
-
 
96
        double rtt = (double)totalTime / (double)numPackets;
-
 
97
        double totalBytes = (double)packetSize * (double)numPackets;
-
 
98
        double bw  = totalBytes / (double)totalTime * (double)1000.0 /* ms */;
-
 
99
 
-
 
100
        System.out.println ("Time for " + numPackets + " packets (millisec): "
-
 
101
                            + totalTime);
-
 
102
 
-
 
103
        System.out.println ("RTT per packet: " + rtt);
-
 
104
        System.out.println ("Total Bandwidth: " + bw + " Bytes / sec");
-
 
105
    }
-
 
106
}
126
}
107
 
127