import java.io.*;
import java.net.*;
import java.util.*;

public class Server {

	public static void main(String[] args) throws IOException {
		DatagramSocket socket = new DatagramSocket(11111);
		
		while (true) {
			try {
				byte[] buf = new byte[256];
				// receive request
				DatagramPacket packet = new DatagramPacket(buf, buf.length);
				socket.receive(packet);
				
				// figure out response
				String dString = null;
				dString = new Date().toString();    // return date
				buf = dString.getBytes();   // convert string to byte array
				
				// send the response to the client at "address" and "port"
				InetAddress address = packet.getAddress();
				int port = packet.getPort();
				packet = new DatagramPacket(buf, buf.length, address, port);
				socket.send(packet);
				System.err.println("server sent (" + dString +") to " + address + ":" + port);
			} catch (IOException e) {
				e.printStackTrace();
				socket.close();
				System.exit(1);
			}
		}
	}
}
