package udp;

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

public class server {
    static byte[] data = new byte[1024];

    static public void main (String args[]) {
	/* Fetch / check argument */
	if (args.length != 2) {
	    System.err.println("Usage: anep.server addr type");
	    System.exit(-1);
	}
	String host = args[0];
	int    port = Integer.parseInt(args[1]);

	System.out.println("Type: " + port);


	/* Build packet (for reception) */
	DatagramPacket packet = new DatagramPacket(data, 0, data.length);

	/* Fetch IP address fo host */
	InetAddress address = null;
	try {
	    address = InetAddress.getByName(host);
	} catch (UnknownHostException e) {
	    System.err.println("FAILED: " + e.toString());
	    System.exit(-1);
	}
	System.out.println("IP  : " + address.getHostAddress());

	/* Receive 1 packet */
	try {
	    /* create socket */
	    DatagramSocket socket = new DatagramSocket(port, address);
	    System.out.println("Listening on: " + 
			       socket.getLocalAddress().getHostAddress());
	    /* Receive packet */
	    socket.receive(packet);
	} catch (Exception e) {
	    System.err.println("FAILED: " + e.toString());
	    System.exit(-1);
	}
	
	/* Print information */
	address = packet.getAddress();
	System.out.println("Received packet from : " + address.getHostName());
	System.out.println("Packet   : " + new String(packet.getData()));
    }
}
