From CS 455 Spring 2013

CS455-S13: Program 2: Ping with TCP and UDP

Assigned: Tuesday, February 19, 2013
Due: Tuesday, March 19, 2013

Contents (hide)

Description

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.

PingClient

PING seqno timestamp
where 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
---- PING Statistics ----
10 packets transmitted, 7 packets received, 30% packet loss
round-trip (ms) min/avg/max = 63/190.29/290
Print the average RTT with up to 2 digits after the decimal.

PingServer

The server's action will either be "not sent" if the ping was ignored or "delayed x ms" .
For TCP, the server will close the client connection after each ping reply is sent.

Rules

Testing

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.

Java Examples

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

Python Examples

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

Notes/FAQs

Java Class Reference
Python Standard Library Reference
No. You must use an actual machine name (atria or sirius). Think about why.
Java: See System.currentTimeMillis()
Python: See time.time()
Java: See the setSoTimeout() function in the DatagramSocket class.
Python: See the settimeout() function in the socket library.
Java: See the DecimalFormat class.
Python: See the str.format() function.
Java:
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);
}
Note: The 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) 
If this evaluates to true, print 'not sent' and don't send the reply.
If this evaluates to false, send the reply.
// Simulate network delay.
delay = (int) (random.nextDouble() * 2 * AVERAGE_DELAY);
Thread.sleep(delay);
Don't forget to print out the delay amount.
Python:
LOSS_RATE = 0.25
AVERAGE_DELAY = 150 # milliseconds
# Create random number generator for use in simulating packet loss and network delay.
random.seed(seed)
Notes:
  • If 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 
If this evaluates to true, print 'not sent' and don't send the reply.
If this evaluates to 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 
Don't forget to print out the delay amount.

Submission

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

Retrieved from http://www.cs.odu.edu/~mweigle/CS455-S13/Prog2
Page last modified on March 05, 2013, at 03:20 PM