Skip to content
Snippets Groups Projects
LokiChatClient.java 3.13 KiB
package MV3500Cohort2020JulySeptember.homework2.Weissenberger;

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

/**
 * This client program establishes a socket connection to the chat server, hand
 * over the connection to a thread.
 *
 * @author Bernd "Loki" Weissenberger
 */
public class LokiChatClient implements Runnable {

    private Socket socket = null;
    private Thread thread = null;
    private DataInputStream console = null;
    private DataOutputStream streamOut = null;
    private LokiClientThread client = null;

    /**
     * constructor. 
     * @param serverName as named (localhost per default)
     * @param serverPort as named (should be 2317)
     */
    public LokiChatClient(String serverName, int serverPort) {
        System.out.println("***********************************************");
        System.out.println("* Establishing connection. Please wait ... :) *");
        System.out.println("***********************************************");

        try {
            socket = new Socket(serverName, serverPort);
            System.out.println("I'm connected: " + socket);
            start();
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println("Unexpected exception (server isn't up??): " + ioe.getMessage());
        }
    }

    public void run() {
        while (thread != null) {
            try {
                streamOut.writeUTF(console.readLine());
                streamOut.flush();
            } catch (IOException ioe) {
                System.out.println("Sending error: " + ioe.getMessage());
                stop();
            }
        }
    }

    public void handle(String msg) {
        if (msg.equals(".bye")) {
            System.out.println("Good bye. Press RETURN to exit ...");
            stop();
        } else {
            System.out.println(msg);
        }
    }
    
    /**
     * setup the streams and (if possible) the Thread
     * @throws IOException input-output error
     */
    public void start() throws IOException {
        console = new DataInputStream(System.in);
        streamOut = new DataOutputStream(socket.getOutputStream());
        if (thread == null) {
            client = new LokiClientThread(this, socket);
            thread = new Thread(this);
            thread.start();
        }
    }
    
    /**
     * stops the Thread
     */
    public void stop() {
        if (thread != null) {  //thread.stop();  
            thread = null;
        }
        try {
            if (console != null) {
                console.close();
            }
            if (streamOut != null) {
                streamOut.close();
            }
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            System.out.println("Error closing ...");
        }
        client.close();
    }
    
    /**
     * main method with fix IP and Port
     * @param args command-line arguments 
     */
    public static void main(String args[]) {
        LokiChatClient client = null;
        client = new LokiChatClient("127.0.0.1", 2317);
    }
}