/*
 * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
import java.io.*;
import java.net.*;

public class KnockKnockClient {
    public static void main(String[] args) {
        try {
            Socket kkSocket = new Socket("taranis", 4444);
            PrintStream os = new PrintStream(kkSocket.getOutputStream());
            DataInputStream is = new DataInputStream(kkSocket.getInputStream());
            StringBuffer buf = new StringBuffer(50);

            int c;
            String fromServer;

            while ((fromServer = is.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye"))
                        break;
                while ((c = System.in.read()) != '\n') {
                    buf.append((char)c);
                }
                System.out.println("Client: " + buf);
                os.println(buf.toString());
                os.flush();
                buf.setLength(0);
            }

            os.close();
            is.close();
            kkSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (Exception e) {
            System.err.println("Exception:  " + e);
        }
    }
}
