From cca61c9f562d2c9e2be13ec16abe134d209b54e3 Mon Sep 17 00:00:00 2001 From: O'Shea Date: Thu, 10 Dec 2020 14:03:54 -0500 Subject: Final implementation, ready for presentation! --- src/chatApp/ClientGUI.java | 109 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/chatApp/ClientGUI.java (limited to 'src/chatApp/ClientGUI.java') diff --git a/src/chatApp/ClientGUI.java b/src/chatApp/ClientGUI.java new file mode 100644 index 0000000..cfb7726 --- /dev/null +++ b/src/chatApp/ClientGUI.java @@ -0,0 +1,109 @@ +package chatApp; + +import java.net.* ; +import java.io.* ; + +public class ClientGUI + { + + static LoginGUI login = new LoginGUI() ; + static ChatGUI chatRoom = new ChatGUI() ; + + @SuppressWarnings( "javadoc" ) + public static void main( String[] args ) throws IOException + { + + login.setVisible( true ); + + boolean done = false ; + while( !done ) + { + done = login.isDone() ; + System.out.print(""); + } + + String ip = login.getIP() ; + String username = login.getUsername() ; + + // Open your connection to a server, at port 6789 + Socket socket = new Socket( ip , 6789 ) ; + + // Create communication streams + DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ; + DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; + + dataStreamOut.writeUTF( username ) ; + + chatRoom.setTitle( "logged in as: " + username ) ; + chatRoom.setVisible( true ) ; + + // Thread for sending messages + Thread sendMessage = new Thread(new Runnable() + { + @Override + public void run() + { + while( true ) + { + try + { + // if-statement is only triggered if client closes window (disconnects) + if( !chatRoom.isVisible() ) + { + dataStreamOut.writeUTF( "/quit" ) ; + dataStreamIn.close() ; + dataStreamOut.close() ; + socket.close() ; + System.exit( 0 ) ; + } + // extract message information from GUI text field + if( chatRoom.messageReady ) + { + String messageOut = chatRoom.getMessageOut() ; + if( messageOut == null ) + { + continue ; + } + dataStreamOut.writeUTF( messageOut ) ; + } + System.out.print("") ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }) ; + + // Thread for receiving messages + Thread receiveMessage = new Thread( new Runnable() + { + + @Override + public void run() + { + while( true ) + { + try + { + String messageIn = dataStreamIn.readUTF() ; + chatRoom.printMessage( messageIn ) ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }); + + // Begin threads + sendMessage.start() ; + receiveMessage.start() ; + + } + } + // end class ClientGUI \ No newline at end of file -- cgit v1.3.1-4-g156e