| 213 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* File: P2_Client.java
|
|
|
3 |
* Author: Ira W. Snyder (devel@irasnyder.com)
|
|
|
4 |
* License: GNU General Public License v2
|
|
|
5 |
* Class: CS380 - Computer Networking
|
|
|
6 |
*
|
|
|
7 |
* Assignment: Project #2
|
|
|
8 |
* Date Last Modified: 2006-02-05
|
|
|
9 |
*
|
|
|
10 |
* Purpose: Implement a simple client allowing the user to send datagrams
|
|
|
11 |
* to P2_Server, which will decode them from 4B to 5B encoding. The client
|
|
|
12 |
* will retransmit any packets that do not recieve a reply.
|
|
|
13 |
******************************************************************************/
|
| 211 |
ira |
14 |
|
|
|
15 |
import java.io.*;
|
|
|
16 |
import java.net.*;
|
|
|
17 |
import java.util.*;
|
|
|
18 |
|
|
|
19 |
public class P2_Client
|
|
|
20 |
{
|
|
|
21 |
private static int soTimeout = 3000; // 3000 ms timeout = 3 sec
|
|
|
22 |
private static DatagramSocket socket;
|
|
|
23 |
private static DatagramPacket packet;
|
|
|
24 |
private static int portNumber = 1337;
|
|
|
25 |
|
| 213 |
ira |
26 |
/**
|
|
|
27 |
* Method: readPacket()
|
|
|
28 |
* Purpose: Read a packet on the currently opened DatagramSocket,
|
|
|
29 |
* and return it as a String.
|
|
|
30 |
*/
|
| 211 |
ira |
31 |
public static String readPacket (int maxlen)
|
|
|
32 |
{
|
|
|
33 |
byte[] buf = new byte[maxlen];
|
|
|
34 |
packet = new DatagramPacket (buf, buf.length);
|
|
|
35 |
|
|
|
36 |
try {
|
|
|
37 |
socket.receive (packet);
|
|
|
38 |
} catch (SocketTimeoutException e) {
|
|
|
39 |
System.out.println ("Read timed out, requesting retransmit");
|
|
|
40 |
return null;
|
|
|
41 |
} catch (PortUnreachableException e) {
|
|
|
42 |
System.out.println ("Caught PortUnreachableException, are you" +
|
|
|
43 |
" sure the server is running?");
|
|
|
44 |
System.exit(1);
|
|
|
45 |
} catch (IOException e) {
|
|
|
46 |
System.out.println ("Caught exception in readPacket()");
|
|
|
47 |
e.printStackTrace();
|
|
|
48 |
return null;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
return new String (buf, 0, packet.getLength()/*- 1*/);
|
|
|
52 |
}
|
| 213 |
ira |
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Method: writePacket()
|
|
|
56 |
* Purpose: Send a packet to localhost:portNumber.
|
|
|
57 |
*/
|
| 211 |
ira |
58 |
public static boolean writePacket (String msg) throws Exception
|
|
|
59 |
{
|
|
|
60 |
InetAddress address = InetAddress.getLocalHost();
|
|
|
61 |
int port = portNumber;
|
|
|
62 |
|
|
|
63 |
byte[] buf = msg.getBytes();
|
|
|
64 |
packet = new DatagramPacket (buf, buf.length, address, port);
|
|
|
65 |
|
|
|
66 |
try {
|
|
|
67 |
socket.send (packet);
|
|
|
68 |
} catch (PortUnreachableException e) {
|
|
|
69 |
System.out.println ("Caught PortUnreachableException, are " +
|
|
|
70 |
"you sure the server is running?");
|
|
|
71 |
System.exit(1);
|
|
|
72 |
} catch (IOException e) {
|
|
|
73 |
System.out.println ("Caught exception in writePacket()");
|
|
|
74 |
e.printStackTrace();
|
|
|
75 |
return false;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return true;
|
|
|
79 |
}
|
|
|
80 |
|
| 213 |
ira |
81 |
/**
|
|
|
82 |
* Method: guaranteedTransmit()
|
|
|
83 |
* Purpose: A wrapper around readPacket() and writePacket() that implements
|
|
|
84 |
* a reliable transmit mechanism. It allows you to specify a maximum number
|
|
|
85 |
* of times to try resending a packet.
|
|
|
86 |
*/
|
|
|
87 |
public static String guaranteedTransmit (String msg, int maxlen, int maxRetries) throws Exception
|
|
|
88 |
{
|
|
|
89 |
String reply = null;
|
|
|
90 |
int retries = 0;
|
|
|
91 |
|
|
|
92 |
writePacket (msg);
|
|
|
93 |
reply = readPacket (maxlen);
|
|
|
94 |
|
|
|
95 |
while (reply == null && retries < maxRetries)
|
|
|
96 |
{
|
|
|
97 |
System.err.println ("Packet lost, retransmitting...");
|
|
|
98 |
writePacket (msg);
|
|
|
99 |
reply = readPacket (maxlen);
|
|
|
100 |
retries++;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return reply;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Method: main()
|
|
|
108 |
* Purpose: This will accept input from the user, and attempt to send it
|
|
|
109 |
* to the corresponding server, which will convert the messages from 4B
|
|
|
110 |
* to 5B encoding.
|
|
|
111 |
*/
|
| 211 |
ira |
112 |
public static void main (String[] args) throws Exception
|
|
|
113 |
{
|
| 212 |
ira |
114 |
int dropHowMany = 0;
|
| 211 |
ira |
115 |
socket = new DatagramSocket();
|
|
|
116 |
socket.connect (InetAddress.getLocalHost(), portNumber);
|
|
|
117 |
socket.setSoTimeout (soTimeout);
|
|
|
118 |
|
|
|
119 |
BufferedReader kbd = new BufferedReader (
|
|
|
120 |
new InputStreamReader (
|
|
|
121 |
System.in));
|
|
|
122 |
|
| 212 |
ira |
123 |
try {
|
|
|
124 |
dropHowMany = Integer.parseInt(args[0]);
|
|
|
125 |
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
126 |
System.err.println ("You need to call the program with \'java P2_Client <num>\'");
|
|
|
127 |
System.exit(1);
|
|
|
128 |
}
|
| 211 |
ira |
129 |
|
| 213 |
ira |
130 |
String msg = new String("DROP" + dropHowMany);
|
|
|
131 |
String reply = guaranteedTransmit (msg, 6, 1);
|
| 212 |
ira |
132 |
|
| 213 |
ira |
133 |
if (!reply.equals(msg))
|
|
|
134 |
{
|
|
|
135 |
System.err.println ("Unable to inform the server how often to drop packets");
|
|
|
136 |
System.exit(1);
|
|
|
137 |
}
|
|
|
138 |
|
| 211 |
ira |
139 |
System.out.print ("% decode: ");
|
| 213 |
ira |
140 |
msg = kbd.readLine();
|
| 211 |
ira |
141 |
boolean done = false;
|
|
|
142 |
|
|
|
143 |
while (!done)
|
|
|
144 |
{
|
| 213 |
ira |
145 |
reply = guaranteedTransmit (msg, 6, 1);
|
| 211 |
ira |
146 |
|
|
|
147 |
if (reply.equals("QUIT"))
|
|
|
148 |
{
|
|
|
149 |
done = true;
|
|
|
150 |
continue;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
System.out.println ("-> reply: " + reply);
|
|
|
154 |
System.out.print ("% decode: ");
|
|
|
155 |
msg = kbd.readLine();
|
|
|
156 |
}
|
| 213 |
ira |
157 |
|
|
|
158 |
// Close the socket
|
|
|
159 |
socket.close();
|
| 211 |
ira |
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|