//
// Simple, command line client to exercise SimpleServer.
// Connects to SimpleServer on 'localhost' at port 5000, 
// no authentication.
//
// Written by Golden G. Richard III, Ph.D., September 1998.
//

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


public class SimpleClient {
    
    private static final int port=5000;
    private static String server = "localhost";
    private static Socket socket=null;       
    private static DataInputStream input=null;  
    private static DataOutputStream output=null; 
    private static InputStreamReader inreader=null;
    private static BufferedReader stdin=null;

    public static void main (String args[]) {

	try {  // handle broken connections

	    // set up connection to server, input, output streams
	    
	    try {  // handle bad host/port errors
		socket = new Socket(server, port);
	    }
	    catch (UnknownHostException e) {
		System.out.println("Unknown IP address for server?");
		System.exit(1);
	    }
	    catch(IOException e) {
		System.out.println("No server found at specified port.");
		System.exit(1);
	    }
	    catch (Exception e) {
		System.out.println("Something strange happened:" + e);
		System.exit(1);
	    }

	    input = new DataInputStream(socket.getInputStream());
	    output = new DataOutputStream(socket.getOutputStream());
	    inreader = new InputStreamReader(System.in);
	    stdin = new BufferedReader(inreader);
	    
	    String what=new String(""), response=null;
	    
	    while (! what.toLowerCase().equals("bye")) {
		
		// expect something from the server, output when it arrives
		
		response = input.readUTF();
		System.out.println("Server said: \"" + response + "\"");
		
		// read a line from standard input and send to server
		
		what = stdin.readLine();
		output.writeUTF(what);
	    }
	}
	catch(IOException e) {
	    System.out.println("Broken connection with server.");
	    System.exit(1);
	}
	catch (Exception e) {
	    System.out.println("Something strange happened:" + e);
	    System.exit(1);
	}
    }
}

