import java.io.*;
import java.net.*;
import java.util.*;

/**
 * An example of receiving a serialized object over the Internet
 * using UDP.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CourseDatabaseReceiver
{

    public static void main(String[] args) throws Exception
    {
        // Construct the UDP socket (for receiving)
        DatagramSocket socket = new DatagramSocket(22801);

        // Construct the UDP datagram
        byte[]         buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        
        // "Wait" to receive a packet
        socket.receive(packet);

        // Construct a ByteArrayInputStream from the payload
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        
        // Decorate the ByteArrayInputStream
        ObjectInputStream in = new ObjectInputStream(bais);

        // Deserialize
        CourseDatabase db = (CourseDatabase)in.readObject();
        in.close();

        // Use the deserialized CourseDatabase
        Iterator<Course> i = db.iterator();
        while (i.hasNext()) 
        {
             Course course = i.next();
             System.out.println(course.getDesignation()+"\t"+
                                course.getTitle());
        }
    }
}
