Subversion Repositories programming

Rev

Rev 186 | Rev 189 | Go to most recent revision | 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
{
22
 
188 ira 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
 
186 ira 32
    public static void main (String[] args) throws IOException
33
    {
188 ira 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();
186 ira 80
    }
188 ira 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
    }
186 ira 106
}
107