Assigned: Tuesday, February 19, 2013
Due: Tuesday, March 19, 2013
Contents (hide)
The goal of this assignment is to help you become familiar with socket programming in Java or Python using both TCP and UDP sockets. We'll be implementing a ping-like client-server application that allows a client to measure the round-trip time (RTT) to a server.
Because we're limited to running the programs on-campus where there will be little delay and very little loss, the server will add some artificial delay and ignore some ping requests (for UDP) to simulate packet loss.
ERR - arg x, where x is the argument number.
PING seqno timestamp
PING is the word "PING", seqno is the ping sequence number (between 0-9), and timestamp is the time (in milliseconds) that the message was created and sent
* if the reply is not received in 1 second (for UDP only).
ping Unix command. Example:
---- PING Statistics ----
10 packets transmitted, 7 packets received, 30% packet loss
round-trip (ms) min/avg/max = 63/190.29/290
long to seed the random number generator for generating delays and lost packets
ERR - arg x, where x is the argument number.
not sent" if the ping was ignored or "delayed x ms" .
A large part of your program's grade will be determined by how well it handles a set of inputs. You should test your program rigorously before submitting. Because your programs will be run and tested using a script, you must format your output exactly as I have described or you will lose points.
Since Java and Python use different random number generators, they will produce different results with the same seed. So, I'm providing separate examples for each language.
Example 1
java PingClient
Usage: java PingClient hostname port protocol
protocol - {TCP, UDP}
java PingServer
Usage: java PingServer port protocol [seed]
protocol - {TCP, UDP}
Example 2
java PingClient atria three TCP ERR - arg 2 java PingServer 10002 tcp ERR - arg 2
Example 3
atria> java PingServer 10002 TCP 123 128.82.4.244:33968> PING 0 1360792272731 ACTION: delayed 216 ms 128.82.4.244:33969> PING 1 1360792272949 ACTION: delayed 297 ms 128.82.4.244:33970> PING 2 1360792273248 ACTION: delayed 75 ms 128.82.4.244:33971> PING 3 1360792273324 ACTION: delayed 182 ms 128.82.4.244:33972> PING 4 1360792273508 ACTION: delayed 241 ms 128.82.4.244:33973> PING 5 1360792273751 ACTION: delayed 262 ms 128.82.4.244:33974> PING 6 1360792274014 ACTION: delayed 214 ms 128.82.4.244:33975> PING 7 1360792274230 ACTION: delayed 21 ms 128.82.4.244:33976> PING 8 1360792274252 ACTION: delayed 238 ms 128.82.4.244:33977> PING 9 1360792274492 ACTION: delayed 173 ms sirius> java PingClient atria 10002 TCP PING 0 1360792272731 RTT: 217 ms PING 1 1360792272949 RTT: 298 ms PING 2 1360792273248 RTT: 76 ms PING 3 1360792273324 RTT: 183 ms PING 4 1360792273508 RTT: 242 ms PING 5 1360792273751 RTT: 263 ms PING 6 1360792274014 RTT: 215 ms PING 7 1360792274230 RTT: 22 ms PING 8 1360792274252 RTT: 239 ms PING 9 1360792274492 RTT: 174 ms ---- PING Statistics ---- 10 packets transmitted, 10 packets received, 0% packet loss round-trip (ms) min/avg/max = 22/192.9/298
Notes:
Example 3
atria> java PingServer 10002 UDP 123 128.82.4.244:44229> PING 0 1360792326564 ACTION: delayed 297 ms 128.82.4.244:44229> PING 1 1360792326863 ACTION: delayed 182 ms 128.82.4.244:44229> PING 2 1360792327046 ACTION: delayed 262 ms 128.82.4.244:44229> PING 3 1360792327309 ACTION: delayed 21 ms 128.82.4.244:44229> PING 4 1360792327331 ACTION: delayed 173 ms 128.82.4.244:44229> PING 5 1360792327505 ACTION: delayed 44 ms 128.82.4.244:44229> PING 6 1360792327550 ACTION: delayed 19 ms 128.82.4.244:44229> PING 7 1360792327570 ACTION: not sent 128.82.4.244:44229> PING 8 1360792328571 ACTION: not sent 128.82.4.244:44229> PING 9 1360792329573 ACTION: delayed 262 ms sirius> java PingClient atria 10002 UDP PING 0 1360792326564 RTT: 299 ms PING 1 1360792326863 RTT: 183 ms PING 2 1360792327046 RTT: 263 ms PING 3 1360792327309 RTT: 22 ms PING 4 1360792327331 RTT: 174 ms PING 5 1360792327505 RTT: 45 ms PING 6 1360792327550 RTT: 20 ms PING 7 1360792327570 RTT: * PING 8 1360792328571 RTT: * PING 9 1360792329573 RTT: 263 ms ---- PING Statistics ---- 10 packets transmitted, 8 packets received, 20% packet loss round-trip (ms) min/avg/max = 20/158.62/299
Example 1
python PingClient.py
Usage: python PingClient.py hostname port protocol
protocol - {TCP, UDP}
python PingServer.py
Usage: python PingServer.py port protocol [seed]
protocol - {TCP, UDP}
Example 2
python PingClient.py atria three TCP ERR - arg 2 python PingServer.py 10002 tcp ERR - arg 2
Example 3
atria> python PingServer.py 10002 TCP 123 128.82.4.244:34747> PING 0 1361211379146 ACTION: delayed 15 ms 128.82.4.244:34748> PING 1 1361211379163 ACTION: delayed 26 ms 128.82.4.244:34749> PING 2 1361211379190 ACTION: delayed 122 ms 128.82.4.244:34750> PING 3 1361211379314 ACTION: delayed 32 ms 128.82.4.244:34751> PING 4 1361211379348 ACTION: delayed 270 ms 128.82.4.244:34752> PING 5 1361211379619 ACTION: delayed 11 ms 128.82.4.244:34753> PING 6 1361211379632 ACTION: delayed 160 ms 128.82.4.244:34754> PING 7 1361211379794 ACTION: delayed 99 ms 128.82.4.244:34755> PING 8 1361211379894 ACTION: delayed 255 ms 128.82.4.244:34756> PING 9 1361211380151 ACTION: delayed 47 ms sirius> python PingClient.py atria 10002 TCP PING 0 1361211379146 RTT: 16 ms PING 1 1361211379163 RTT: 26 ms PING 2 1361211379190 RTT: 123 ms PING 3 1361211379314 RTT: 33 ms PING 4 1361211379348 RTT: 271 ms PING 5 1361211379619 RTT: 12 ms PING 6 1361211379632 RTT: 161 ms PING 7 1361211379794 RTT: 99 ms PING 8 1361211379894 RTT: 256 ms PING 9 1361211380151 RTT: 48 ms ---- PING Statistics ---- 10 packets transmitted, 10 packets received, 0.0% packet loss round-trip (ms) min/avg/max = 12/104.50/271
Notes:
Example 3 - edited 3/5/13 -MCW
atria> python PingServer.py 10002 UDP 123 128.82.4.244:58353> PING 0 1361211436430 ACTION: not sent 128.82.4.244:58353> PING 1 1361211437433 ACTION: not sent 128.82.4.244:58353> PING 2 1361211438435 ACTION: delayed 32 ms 128.82.4.244:58353> PING 3 1361211438468 ACTION: delayed 11 ms 128.82.4.244:58353> PING 4 1361211438480 ACTION: delayed 99 ms 128.82.4.244:58353> PING 5 1361211438580 ACTION: delayed 47 ms 128.82.4.244:58353> PING 6 1361211438628 ACTION: delayed 100 ms 128.82.4.244:58353> PING 7 1361211438729 ACTION: not sent 128.82.4.244:58353> PING 8 1361211439731 ACTION: not sent 128.82.4.244:58353> PING 9 1361211440733 ACTION: delayed 26 ms sirius> python PingClient.py atria 10002 UDP PING 0 1361211436430 RTT: * PING 1 1361211437433 RTT: * PING 2 1361211438435 RTT: 33 ms PING 3 1361211438468 RTT: 12 ms PING 4 1361211438480 RTT: 100 ms PING 5 1361211438580 RTT: 48 ms PING 6 1361211438628 RTT: 101 ms PING 7 1361211438729 RTT: * PING 8 1361211439731 RTT: * PING 9 1361211440733 RTT: 27 ms ---- PING Statistics ---- 10 packets transmitted, 6 packets received, 40.0% packet loss round-trip (ms) min/avg/max = 12/53.50/101
System.currentTimeMillis()
time.time()
setSoTimeout() function in the DatagramSocket class.
settimeout() function in the socket library.
DecimalFormat class.
str.format() function.
java.util.Random class (so put import java.util.* at the beginning of your program).
class PingServer { and public static void main lines, insert:
private static final double LOSS_RATE = 0.25; private static final int AVERAGE_DELAY = 150; // milliseconds
// Create random number generator for use in simulating packet loss and network delay.
Random random;
if (seed == 0) {
random = new Random();
} else {
random = new Random(seed);
}
Random object should be created only once in the server program. Do not create new Random objects for each connection or for each packet received.
// Decide whether to reply, or simulate packet loss. if (random.nextDouble() < LOSS_RATE)
true, print 'not sent' and don't send the reply.
false, send the reply.
// Simulate network delay. delay = (int) (random.nextDouble() * 2 * AVERAGE_DELAY); Thread.sleep(delay);
random (so put import random at the beginning of your program).
LOSS_RATE = 0.25 AVERAGE_DELAY = 150 # milliseconds
# Create random number generator for use in simulating packet loss and network delay. random.seed(seed)
seed == 0, the seed will be set based on the current time of day. This will produce different values each time you run the program.
random.seed() should be called only once in the server program. Do not reset the seed for each connection or for each packet received.
# Decide whether to reply, or simulate packet loss. if random.random() < LOSS_RATE
true, print 'not sent' and don't send the reply.
false, send the reply.
# Simulate network delay. delay = (random.random() * 2 * AVERAGE_DELAY) # sleep time in ms time.sleep(delay/1000.0) # accepts time in secs
You must name your source files PingClient.java and PingServer.java (note the capitalization) or PingClient.py and PingServer.py. Make sure that you submit all files necessary to compile your program. But, do not submit compiled files (.class files).
Directions for submitting your assignment through Blackboard