/*
 * The MIT License
 *
 * Copyright 2016 Andrew Simmons
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.github.agsimmons.jtictactoe.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client {

    private static final int DEFAULT_PORT = 8888;

    private Socket clientSocket;

    private DataInputStream recieve;
    private DataOutputStream send;

    public Client() {
        createSocket();
        initializeIOStreams();
        gameLoop();
    }

    private void createSocket() {
        Scanner scanner = new Scanner(System.in);
        String serverIP;
        String serverPort;
        int port = 0;

        boolean isValidAddress = false;
        do {
            boolean addressChosen = false;
            do {

                System.out.print("Server IP: ");
                serverIP = scanner.nextLine();

                System.out.print("Server Port (8888): ");
                serverPort = scanner.nextLine();
                if (serverPort.equals("")) {
                    port = DEFAULT_PORT;
                    addressChosen = true;
                } else {
                    try {
                        port = Integer.parseInt(serverPort);
                        addressChosen = true;
                    } catch (NumberFormatException e) {
                        System.out.println("ERROR: Invalid port!");
                    }
                }

            } while (!addressChosen);

            try {
                clientSocket = new Socket(serverIP, port);
                isValidAddress = true;
            } catch (IOException ex) {
                System.out.println("ERROR: Could not create socket to server!");
            }

        } while (!isValidAddress);
    }

    private void initializeIOStreams() {
        try {
            recieve = new DataInputStream(clientSocket.getInputStream());
            send = new DataOutputStream(clientSocket.getOutputStream());
        } catch (IOException ex) {
            System.out.println("ERROR: Could not create IO streams!");
            System.exit(1);
        }
    }

    private void gameLoop() {
        System.out.println(recieveMessage());
    }

    private String recieveMessage() {
        String returnMessage = "";

        try {
            returnMessage = recieve.readUTF();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

        return returnMessage;
    }

}

@@ -1325,73 +1325,8 @@
 ner;
-%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Apu
@@ -1502,16 +1502,43 @@
 am send;
+%0A    private Scanner input;
 %0A%0A    pu
@@ -3089,32 +3089,76 @@
 utputStream());%0A
+            input = new Scanner(System.in);%0A
         %7D catch 
@@ -3330,35 +3330,109 @@
         
-System.out.println(
+boolean gameInProgress = true;%0A        while (gameInProgress) %7B%0A            gameInProgress = 
 recieveM
@@ -3439,19 +3439,28 @@
 essage()
-)
 ;%0A
+        %7D%0A
     %7D%0A%0A 
@@ -3470,22 +3470,23 @@
 private 
-String
+boolean
  recieve
@@ -3493,24 +3493,42 @@
 Message() %7B%0A
+        try %7B%0A    
         Stri
@@ -3550,73 +3550,338 @@
 e = 
-%22%22;%0A%0A        try %7B%0A            returnMessage = recieve.readUTF();
+recieve.readUTF();%0A%0A            switch (returnMessage) %7B%0A                case %22RESPOND%22:%0A                    respond();%0A                    break;%0A                case %22END%22:%0A                    return false;%0A                default:%0A                    System.out.println(returnMessage);%0A                    break;%0A            %7D%0A
 %0A   
@@ -3928,118 +3928,403 @@
     
-Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D%0A%0A        return returnMessage;
+System.out.println(%22ERROR: Could not recieve message!%22);%0A            System.exit(1);%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    private void respond() %7B%0A        try %7B%0A            System.out.print(%22Response: %22);%0A            send.writeUTF(input.nextLine());%0A        %7D catch (IOException ex) %7B%0A            System.out.println(%22ERROR: Could not send response!%22);%0A            System.exit(1);%0A        %7D
 %0A   

Implement client functionality to send & receive messages and quit