package anep;

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

public class client {
    static byte[] data = "Hello JAVA ANEP World\0".getBytes();
    static byte[] opt1 = "ANEP option Number 15\0".getBytes();
    static byte[] opt2 = "I'm option 16\0"        .getBytes();

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

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

	/* Summary some informations */
	System.out.println("Host: " + host);
	System.out.println("IP  : " + address.getHostAddress());
	System.out.println("Type: " + type);

	/* Build ANEP packet with options */
	ANEPOption[] options = new ANEPOption[2];
	options[0] = new ANEPOption(15, opt1, 0, opt1.length);
	options[1] = new ANEPOption(16, opt2, 0, opt2.length);

	ANEPPacket packet = new ANEPPacket(type, 
					   data, 0, data.length, 
					   options, address);

	/* Send 1 packet */
	try {
	    /* create socket */
	    ANEPSocket socket = new ANEPSocket(); // sending only (no type)

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