summaryrefslogtreecommitdiff
path: root/src/chatApp/ClientGUI.java
diff options
context:
space:
mode:
authorO'Shea <osheas1@wit.edu>2020-12-10 14:03:54 -0500
committerO'Shea <osheas1@wit.edu>2020-12-10 14:03:54 -0500
commitcca61c9f562d2c9e2be13ec16abe134d209b54e3 (patch)
tree3f957b579c45d0536e7e1740acdb0cdbc773f9f4 /src/chatApp/ClientGUI.java
parent690bbba9550e2644e48db44fb94d4df86494a34e (diff)
Final implementation, ready for presentation!
Diffstat (limited to 'src/chatApp/ClientGUI.java')
-rw-r--r--src/chatApp/ClientGUI.java109
1 files changed, 109 insertions, 0 deletions
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