package MV3500Cohort2020JulySeptember.homework2.Goericke;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 *
 * @author stefa
 */
public class GoerickeClient {

    // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1
    public final static String LOCALHOST = "0:0:0:0:0:0:0:1";

    public static void main(String[] args) {
        
        // Local vars/fields
        Socket socket;
        InputStream is;
        InputStreamReader isr;
        BufferedReader br;
        String serverMessage;
        
        try {
            while (true) {
                System.out.println("\nGoerickeClient creating socket...");

                // We request an IP to connect to ("localhost") and
                // port number at that IP (2317). This establishes
                // a connection to that IP in the form of a Socket
                // object; the server uses a ServerSocket to wait for
                // connections.
                socket = new Socket(LOCALHOST, 2317); // locohost?

                // thats my input stream 
                is = socket.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);

                // Read a single line written by the server. We'd
                // do things a bit differently if there were many lines to be read
                // from the server instead of one only.
                serverMessage = br.readLine();
                System.out.println("The message the server sent was: '" + serverMessage + "'");
                // socket gets closed, either automatically/silently by this code (or possibly by the server)
                if (serverMessage.contentEquals("Welcome, how are you !!!")) {
                    System.out.println("What a nice and friendly server");
                } else if (serverMessage.contentEquals("Still alive there ?")){
                    System.out.println("Yes, I am ... still .... where else should I be");
                } else{
                    System.out.println("Seems he does not have anything to tell");
                }
                
            } // end while(true)
        } catch (IOException e) {
            System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening
            System.err.println("Error: " + e);
            
            // Provide more helpful information to user if exception occurs due to running twice at one time
            if (e instanceof java.net.BindException) {
                System.err.println("*** Be sure to stop any other running instances of programs using this port!");
            }
        } finally {
        
            // program exit: tell somebody about that
            System.out.println("\nclient exit");
        }
    }
}