CS 455/555 - Intro to Networks and Communication
Spring 2011: Tues/Thurs 9:30-10:45am, Spong 108

Print - Admin




Final Paper



Program 2: Ping with TCP and UDP

Assigned: Tuesday, February 15, 2011
Due: Tuesday, March 15, 2011


The goal of this assignment is to help you become familiar with socket programming in Java 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.


  • The ping client will accept three command-line arguments: hostname, port, and protocol.
    • hostname - the name of the server
    • port - the port the server is running on
    Note that for ODU-CS machines, the port must be between 10001-11000. Also, these ports are only accessible to clients that are on campus.
    • protocol - either TCP or UDP (all caps)
  • As with the Program 1, if any of the arguments are incorrect, exit after printing an error message of the form ERR - arg x, where x is the argument number.
  • Depending on the protocol given, the ping client will either setup a TCP connection to send each message or send UDP datagrams. For TCP, this application-level protocol is non-persistent, meaning that there is one request-reply exchange per TCP connection.
  • The client will send 10 messages to the server in the following format:
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
  • The client will not send a new ping until the previous ping has been answered.
  • On a single line, the client will print the message that it sends to the server and the round-trip time for the ping or a * if the reply is not received in 1 second (for UDP only).
  • After receiving the 10th reply (or after its timeout), the client will print a summary with a format similar to that displayed by the 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
Print the average RTT with up to 2 digits after the decimal (see Java's DecimalFormat class).


  • The ping server is essentially an echo server (whatever data is sent will be returned).
  • The ping server will accept 2 required command-line arguments: port and protocol.
    • port - the port the server is running on
    • protocol - either TCP or UDP (all caps)
  • The ping server will accept 1 optional command-line argument: seed.
    • seed - a long to seed the random number generator for generating delays and lost packets
  • As with the Program 1, if any of the arguments are incorrect, exit after printing an error message of the form ERR - arg x, where x is the argument number.
  • After a ping request has been received by the UDP server, the server will determine if it should ignore the packet or respond to it (simulating losses). Default loss rate is 25%.
  • Before responding to the client, the server will wait for a random amount of time to simulate network delay (average 150 ms each way).
  • On a single line, the server will print the IP address and port of the client, the client's ping message, and the server's action.
The server's action will either be "not sent" if the ping was ignored or "delayed x ms" .
  • The ping server will keep running until the user presses Ctrl-C.
For TCP, the server will close the client connection after each ping reply is sent.


  • As with all projects, you are not permitted to work with anyone else (even students not in the class) - all of the coding and documentation must be your own.
  • Your programs must compile and run on the CS Unix machines.
  • You must write neat code and document it well. You will lose points for sloppy programs that contain little or no comments.


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.

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 vega three TCP
ERR - arg 2

java PingServer 10002 tcp
ERR - arg 2

Example 3

vega> java PingServer 10002 TCP 123> PING 0 1297357012973  ACTION: delayed 216 ms> PING 1 1297357013192  ACTION: delayed 297 ms> PING 2 1297357013496  ACTION: delayed 75 ms> PING 3 1297357013576  ACTION: delayed 182 ms> PING 4 1297357013760  ACTION: delayed 241 ms> PING 5 1297357014002  ACTION: delayed 262 ms> PING 6 1297357014265  ACTION: delayed 214 ms> PING 7 1297357014481  ACTION: delayed 21 ms> PING 8 1297357014502  ACTION: delayed 238 ms> PING 9 1297357014742  ACTION: delayed 173 ms

procyon>  java PingClient vega 10002 TCP 
PING 0 1297357012973  RTT: 218 ms
PING 1 1297357013192  RTT: 298 ms
PING 2 1297357013496  RTT: 75 ms
PING 3 1297357013576  RTT: 183 ms
PING 4 1297357013760  RTT: 241 ms
PING 5 1297357014002  RTT: 263 ms
PING 6 1297357014265  RTT: 215 ms
PING 7 1297357014481  RTT: 21 ms
PING 8 1297357014502  RTT: 239 ms
PING 9 1297357014742  RTT: 173 ms
---- PING Statistics ----
10 packets transmitted, 10 packets received, 0% packet loss
round-trip (ms) min/avg/max = 21/192.6/298

Note: The delays the server uses should be the same with the same seed, but the timestamps in the client's ping message will not since they depend on the time the program was run. Also, with TCP since you are creating a new connection for each PING sent, the client's port number will be different each time. Also, the RTTs that the client reports should be similar to the example, but may not be exact.

Example 3

vega> java PingServer 10002 UDP 123> PING 0 1297357083680  ACTION: delayed 297 ms> PING 1 1297357083979  ACTION: delayed 182 ms> PING 2 1297357084161  ACTION: delayed 262 ms> PING 3 1297357084423  ACTION: delayed 21 ms> PING 4 1297357084445  ACTION: delayed 173 ms> PING 5 1297357084618  ACTION: delayed 44 ms> PING 6 1297357084662  ACTION: delayed 19 ms> PING 7 1297357084681  ACTION: not sent> PING 8 1297357085682  ACTION: not sent> PING 9 1297357086682  ACTION: delayed 262 ms

procyon> java PingClient vega 10002 UDP
PING 0 1297357083680  RTT: 299 ms
PING 1 1297357083979  RTT: 182 ms
PING 2 1297357084161  RTT: 262 ms
PING 3 1297357084423  RTT: 22 ms
PING 4 1297357084445  RTT: 173 ms
PING 5 1297357084618  RTT: 44 ms
PING 6 1297357084662  RTT: 19 ms
PING 7 1297357084681  RTT: *
PING 8 1297357085682  RTT: *
PING 9 1297357086682  RTT: 263 ms
---- PING Statistics ----
10 packets transmitted, 8 packets received, 20% packet loss
round-trip (ms) min/avg/max = 19/158/299


  • Can I use the fast.cs.odu.edu alias when running these programs?
No. You must use an actual machine name (antares, vega, capella, or procyon).
  • How can I get the current time in milliseconds?
See System.currentTimeMillis()
  • How can I set a timeout value on reading from a datagram socket?
See the setSoTimeout() function in the DatagramSocket class in the Java Class Reference (the link is under 'Useful Links' on the course webpage).
  • How can I format a double to have a certain number of digits after the decimal?
See the DecimalFormat class in the Java Class Reference.
  • How can I generate a random loss pattern and artificial delay?
    • You'll use the java.util.Random class (so put import java.util.* at the beginning of your program).
    • Between your 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
    • To set the random number generator seed and create the Random object (after you have determined if the user has given you a seed argument), use
    // 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.
    • To determine whether to reply to a ping, use
    // 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.
    • To add delay, use
    // Simulate network delay.
    delay = (int) (random.nextDouble() * 2 * AVERAGE_DELAY);
    Don't forget to print out the delay amount.


You must name your source files PingClient.java and PingServer.java (note the capitalization). 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