package udp;

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

public class client {
    static byte[] data = "Hello JAVA UDP World\0".getBytes();

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

	System.out.println("Host: " + host);
	System.out.println("Port: " + port);


	/* 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());


	/* Build Datagram packet with options */
	DatagramPacket packet = new DatagramPacket(data, 0, data.length, 
					   address, port);

	/* Send 1 packet */
	DatagramSocket socket = null;
	try {
	    /* create socket */
	    socket = new DatagramSocket(); // sending only (no port)

	    /* send packet */
	    socket.send(packet);
	} catch (Exception e) {
	    System.err.println("FAILED: " + e.toString());
	    System.exit(-1);
	}
    }
}
