import java.io.*; import java.net.*; import java.awt.*; // ---------------------------------------------- // This thread reads in input stream from a socket and // appends the output to a TextArea object class DisplayThread extends Thread { TextArea m_text; BufferedReader inChat = null; DisplayThread (TextArea text, BufferedReader in) { m_text =text; inChat = in; } public void run() { System.out.println("Start DisplayThread "); String str; try { while ((str = inChat.readLine()) != null) { if (str.length() > 0) { m_text.append(str); m_text.append("\n"); } } } catch (IOException e) { System.out.println("inChat received an IOException: "+ e.getMessage()); e.printStackTrace(); } System.out.println("Display Thread Finishing"); } } // ----------------------------------------- public class ChatClient extends Frame{ TextArea tArea=new TextArea("",15,80,TextArea.SCROLLBARS_BOTH); ChatClient(){ add(tArea); } public static void main(String[] args) throws IOException { ChatClient myFrame=new ChatClient(); Socket chatSocket = null; PrintWriter outChat = null; BufferedReader inChat = null; int port =4444; InetAddress localAddr = InetAddress.getLocalHost(); DisplayThread myReader; MyInput inKeyboard = new MyInput(); String str; System.out.println("Your Local Host="+localAddr); // // The default connection is to your local host and port 4444 // If you want something different, the first parameter is the // Ip Address of the desired server, the second parameter is the // port number. // For example: // java ChatClient ---> connects to your local host and port 4444 // java ChatClient 163.215.77.8 connects to the specified server, port 4444 // java ChatClient 163.215.77.8 8000 connects to port 8000 on server // 163.215.77.8 try { if (args.length > 1) port = Integer.parseInt(args[1]); } catch (ArithmeticException e) { System.out.println("Second argument is not an integer"); } try { if (args.length == 0) chatSocket = new Socket(localAddr, port); else chatSocket = new Socket(args[0], port); // Create a PrintWriter object for socket output outChat = new PrintWriter( chatSocket.getOutputStream(), true); // Create a BufferedReader object for socket input inChat = new BufferedReader( new InputStreamReader( chatSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + localAddr); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: " + localAddr + " message : " + e.getMessage()); e.printStackTrace(); System.exit(1); } myReader = new DisplayThread(myFrame.tArea, inChat); myReader.start(); // Must resize the frame before it can be shown myFrame.setSize(300, 300); // Make the frame appear on the screen myFrame.show(); for (;;) { str = inKeyboard.get(); if (str == null) break; if (str.equals("####")) break; System.out.println(str); if (myReader.isAlive()) outChat.println(str); else { System.out.println("Chat reader died ... probably the server is down"); break; } } System.out.println("Terminating"); outChat.close(); inChat.close(); chatSocket.close(); System.exit(0); } }