CS 455/555 - Intro to Networks and Communications
Fall 2009: Tues/Thurs 3-4:15pm, Spong 0108

Print - Admin



  • Dr. Michele Weigle
  • mweigle at cs.odu.edu
  • E&CS 3214
  • Office Hours: MW 2:30-4pm
  • Grader: Hui Shi
  • hshi at cs.odu.edu


Syllabus (pdf)

Useful Links

Program 2: Ping with TCP and UDP

Assigned: Thursday, September 24, 2009
Due: Thursday, October 15, 2009 before class

Description Rules Testing Hints Submission


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 and send messages or send UDP datagrams.
  • 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).


  • 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).
  • Before responding to the client, the server will wait for a random amount of time to simulate network delay.
  • 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 1234821840436  ACTION: delayed 144 ms> PING 1 1234821840596  ACTION: delayed 198 ms> PING 2 1234821840805  ACTION: delayed 50 ms> PING 3 1234821840865  ACTION: delayed 121 ms> PING 4 1234821840995  ACTION: delayed 161 ms> PING 5 1234821841165  ACTION: delayed 175 ms> PING 6 1234821841345  ACTION: delayed 143 ms> PING 7 1234821841495  ACTION: delayed 14 ms> PING 8 1234821841515  ACTION: delayed 159 ms> PING 9 1234821841685  ACTION: delayed 115 ms

antares>  java PingClient vega 10002 TCP 
PING 0 1234821840436  RTT: 159 ms
PING 1 1234821840596  RTT: 208 ms
PING 2 1234821840805  RTT: 59 ms
PING 3 1234821840865  RTT: 130 ms
PING 4 1234821840995  RTT: 169 ms
PING 5 1234821841165  RTT: 179 ms
PING 6 1234821841345  RTT: 149 ms
PING 7 1234821841495  RTT: 19 ms
PING 8 1234821841515  RTT: 169 ms
PING 9 1234821841685  RTT: 119 ms

(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.)

Example 3

vega> java PingServer 10002 UDP 123> PING 0 1234821943582  ACTION: delayed 198 ms> PING 1 1234821943786  ACTION: not sent> PING 2 1234821944791  ACTION: delayed 161 ms> PING 3 1234821944955  ACTION: delayed 143 ms> PING 4 1234821945105  ACTION: not sent> PING 5 1234821946110  ACTION: delayed 115 ms> PING 6 1234821946235  ACTION: delayed 29 ms> PING 7 1234821946275  ACTION: delayed 13 ms> PING 8 1234821946295  ACTION: not sent> PING 9 1234821947300  ACTION: not sent

antares> java PingClient vega 10002 UDP
PING 0 1234821943582  RTT: 204 ms
PING 1 1234821943786  RTT: *
PING 2 1234821944791  RTT: 164 ms
PING 3 1234821944955  RTT: 150 ms
PING 4 1234821945105  RTT: *
PING 5 1234821946110  RTT: 125 ms
PING 6 1234821946235  RTT: 40 ms
PING 7 1234821946275  RTT: 20 ms
PING 8 1234821946295  RTT: *
PING 9 1234821947300  RTT: *


  • Can I use the fast@cs.odu.edu alias when running these programs?
No. You must use an actual machine name (antares, vega, or hydra).
  • 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 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.3;
    private static final int AVERAGE_DELAY = 100; // 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);
    • 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