CPSC 441, Fall 2014
Lab 5: Programming UDP

The assignment for this lab is to write a UDP server and a matching UDP client. The specifications are given below, along with some information about programming UDP in Java. You should submit your work into your homework folder in /classes/cs441/homework by next Friday.

The Programs

To give you something definite to work on, you should write UDP client/server programs for looking up zip code information. The file /classes/cs441/zipcodes.txt contains the information that you need. Each line of the file has a zip code, a latitude, a longitude, a city name, and a state name. The five data fields are separated by commas. (This makes it easy to break a line into its five parts by using line.split(",").)

Your server program should begin by reading the file and storing the information that it contains in some kind of data structure. I suggest creating a simple class to hold the data for one zip code. Store the objects in a HashMap, using the zip code as a key. Depending on what exactly you want your server to do, you could use additional HashMaps or other data structures for looking up information based on other keys. (Since the file is quite long, I suggest that you read the file directly from its original location, rather than make a copy.)

Your server will process requests from clients in an infinite loop. The client will send a request that the server look up some zip code information, and the server will send back a response. For example, the client could send a numerical zip code and the server could send back the name of the city and state that uses that zip code, or an error message if the zip code does not exist. That would be a very minimal server. A more reasonable server would allow several types of queries. For example, if sent a city name, the server could return all zip codes for cities with that name.

In addition to the server, you should write a client program (or several client programs for different kinds of queries). The programs should make it reasonably easy for a user to query the zip code server.

You will need to design a protocol that the client and server can use for communication. This is an application-level protocol that you are making up, and the details are all up to you. Make sure that your protocol is well-documented in the comments on your server. Also be sure to include comments in the client program that explain how to use it.

UDP Programming in Java

You can check out http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html for the official tutorial on "Datagram" client/server programming in Java. I should leave it at that. However, I did write an example of my own, which you can find in the files Server.java and Client.java in the folder /classes/cs441/udp. And I will document some of the basic API that you need here. The main classes that you need to work with are DatagramSocket, DatagramPacket, and InetAddress. A DatagramSocket is used for sending and receiving UDP datagrams, which are represented by the DatagramPacket class. There is no distinction between client and server sockets; the difference is in how they are used. You have already seen the InetAddress class, which represents an IP address, in connection with TCP. Here are some API calls, with comments about how they are used.