summaryrefslogtreecommitdiff
path: root/src/chatApp/ClientGUI.java
blob: cfb77261ecf1059711fb0b7570941d9766f917c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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