Subversion Repositories programming

Rev

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

Rev 186 Rev 188
Line 13... Line 13...
13
 *
13
 *
14
 ******************************************************************************/
14
 ******************************************************************************/
15
 
15
 
16
import java.io.*;
16
import java.io.*;
17
import java.net.*;
17
import java.net.*;
-
 
18
import java.util.Date;
18
 
19
 
19
public class P1Client
20
public class P1Client
20
{
21
{
21
 
22
 
-
 
23
    public static final String exitSeq = "QUIT";
-
 
24
    public static final String hostName = "localhost";
-
 
25
    public static final int portNumber = 1337;
-
 
26
    public static final int numPackets = 200000;
-
 
27
    public static final int packetSize = 1024;
-
 
28
 
-
 
29
    private static Date startTime;
-
 
30
    private static Date stopTime;
-
 
31
    
22
    public static void main (String[] args) throws IOException
32
    public static void main (String[] args) throws IOException
23
    {
33
    {
-
 
34
        Socket sendSocket = null;
-
 
35
        PrintWriter out = null;
-
 
36
        BufferedReader in = null;
-
 
37
 
-
 
38
        try
-
 
39
        {
-
 
40
            sendSocket = new Socket (hostName, portNumber);
-
 
41
            out = new PrintWriter (sendSocket.getOutputStream(), true);
-
 
42
            in = new BufferedReader (new InputStreamReader(
-
 
43
                                         sendSocket.getInputStream()));
-
 
44
        }
-
 
45
        catch (UnknownHostException e)
-
 
46
        {
-
 
47
            System.err.println ("Can't resolve hostname: " + hostName);
-
 
48
            System.exit(1);
-
 
49
        }
-
 
50
        catch (IOException e)
-
 
51
        {
-
 
52
            System.err.println ("Couldn't get I/O for " +
-
 
53
                                "the connection to: " + hostName);
-
 
54
            System.exit(1);
-
 
55
        }
-
 
56
 
-
 
57
        String packet = generatePacket (packetSize);
-
 
58
        String response = new String();
-
 
59
        int i = 0;
-
 
60
 
-
 
61
        /* Start the timer */
-
 
62
        startTime = new Date();
-
 
63
        
-
 
64
        for (i=0; i<numPackets; i++)
-
 
65
        {
-
 
66
            out.println (packet);
-
 
67
            response = in.readLine();
-
 
68
        }
-
 
69
 
-
 
70
        /* Stop the timer */
-
 
71
        stopTime = new Date();
-
 
72
 
-
 
73
        /* Calculate and print all timing info */
-
 
74
        printTimingInfo(startTime, stopTime);
-
 
75
 
-
 
76
        /* Cleanup */
-
 
77
        out.close();
-
 
78
        in.close();
-
 
79
        sendSocket.close();
-
 
80
    }
-
 
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");
24
    }
105
    }
25
    
-
 
26
}
106
}
27
 
107