package ss.week7.cmdline;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Client class for a simple client-server application
 * @author  Theo Ruys
 * @version 2005.02.21
 */
public class Client {
	private static final String USAGE = "usage: java week4.cmdline.Client <name> <address> <port>";

	/** Start een Client-applicatie op. */
	public static void main(String[] args) {
		if (args.length != 3) {
			System.out.println(USAGE);
			System.exit(0);
		}

		String name = args[0];
		InetAddress addr = null;
		int port = 0;
		Socket sock = null;

		// check args[1] - the IP-adress
		try {
			addr = InetAddress.getByName(args[1]);
		} catch (UnknownHostException e) {
			System.out.println(USAGE);
			System.out.println("ERROR: host " + args[1] + " unknown");
			System.exit(0);
		}

		// parse args[2] - the port
		try {
			port = Integer.parseInt(args[2]);
		} catch (NumberFormatException e) {
			System.out.println(USAGE);
			System.out.println("ERROR: port " + args[2] + " is not an integer");
			System.exit(0);
		}

		// try to open a Socket to the server
		try {
			sock = new Socket(addr, port);
		} catch (IOException e) {
			System.out.println("ERROR: could not create a socket on " + addr
					+ " and port " + port);
		}

		// create Peer object and start the two-way communication
		try {
			Peer client = new Peer(name, sock);
			Thread streamInputHandler = new Thread(client);
			streamInputHandler.start();
			client.handleTerminalInput();
			System.out.println("komt ie heir?");
			client.shutDown();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

} // end of class Client

/** Start een Client-applicatie op. */
package ss.week7.cmdline;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Client class for a simple client-server application
 * @author  Theo Ruys
 * @version 2005.02.21
 */
public class Client {
	private static final String USAGE = "usage: java week4.cmdline.Client <name> <address> <port>";

	/** Start een Client-applicatie<SUF>*/
	public static void main(String[] args) {
		if (args.length != 3) {
			System.out.println(USAGE);
			System.exit(0);
		}

		String name = args[0];
		InetAddress addr = null;
		int port = 0;
		Socket sock = null;

		// check args[1] - the IP-adress
		try {
			addr = InetAddress.getByName(args[1]);
		} catch (UnknownHostException e) {
			System.out.println(USAGE);
			System.out.println("ERROR: host " + args[1] + " unknown");
			System.exit(0);
		}

		// parse args[2] - the port
		try {
			port = Integer.parseInt(args[2]);
		} catch (NumberFormatException e) {
			System.out.println(USAGE);
			System.out.println("ERROR: port " + args[2] + " is not an integer");
			System.exit(0);
		}

		// try to open a Socket to the server
		try {
			sock = new Socket(addr, port);
		} catch (IOException e) {
			System.out.println("ERROR: could not create a socket on " + addr
					+ " and port " + port);
		}

		// create Peer object and start the two-way communication
		try {
			Peer client = new Peer(name, sock);
			Thread streamInputHandler = new Thread(client);
			streamInputHandler.start();
			client.handleTerminalInput();
			System.out.println("komt ie heir?");
			client.shutDown();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

} // end of class Client
