/** * Chat.java * * A simple chat program, both client and server in a single * class, to demonstrate power of java (written in an evening). * * @author Chip Watson */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Chat implements Runnable, ActionListener, WindowListener { Frame frame; TextArea text; TextField textInput; TextField timer; BufferedReader input; OutputStreamWriter output; boolean gui; boolean active; static long started; public static void main (String [] argv) { boolean server = false; boolean makegui = true; String target = null; if (argv.length >= 1 && argv[0].equals("-s")) { server = true; if (argv.length == 2 && argv[1].equals("-e")) makegui = false; } else if (argv.length == 1) { target = argv[0]; } Chat chat = new Chat(server, makegui, target); } public Chat(boolean server, boolean makegui, String target) { gui = makegui; if (gui) setupGUI(); if (server) { ServerSocket listen; try { listen = new ServerSocket(7007); } catch (IOException e) { System.out.println(e.toString()); return; } while (true) { try { Socket client = listen.accept(); System.out.println("accepted " + client.toString()); handleSocket(client); } catch (Exception e) { System.out.println(e); } } } else { // client // a thread to keep track of connect time started = System.currentTimeMillis(); Thread thread = new Thread(this); thread.start(); try { InetAddress ia = InetAddress.getByName(target); System.out.println("connecting to "+ia+" ...\n"); Socket sock = new Socket(ia,7007); handleSocket(sock); } catch (Exception e) { System.out.println(e); } } } public void setupGUI() { frame = new Frame("Chip's Chat"); frame.setLayout(new FlowLayout()); frame.addWindowListener(this); text = new TextArea(16,80); frame.setLayout(new FlowLayout()); text.setEditable(false); frame.add(text); Panel lower = new Panel(); lower.setSize(590,120); timer = new TextField(6); lower.add(timer); textInput = new TextField(60); textInput.addActionListener(this); lower.add(textInput); frame.add(lower); frame.pack(); frame.validate(); frame.setSize(600,380); frame.validate(); } public void handleSocket(Socket client) { System.out.println("gui = "+gui); try { InputStreamReader isr; isr = new InputStreamReader(client.getInputStream()); input = new BufferedReader(isr); output = new OutputStreamWriter(client.getOutputStream()); } catch (IOException e) { if (gui) text.setText(e.toString()); return; } active = true; if (gui) { text.setText("connection to \n "+client.toString()); frame.setVisible(true); } try { while (active) { String line = input.readLine(); if (line == null) System.out.println("null received"); if (gui) { text.append("\n"); text.append(line); } else { output.write(line); output.write("\n"); output.flush(); } } if (gui) frame.setVisible(false); } catch (IOException e) { if (gui) text.append(e.toString()); } } public void run() { while (true) { try {Thread.sleep(1000);} catch (Exception e) {} long elapsed = (System.currentTimeMillis() - started)/1000; timer.setText(new Long(elapsed).toString()); } } public void actionPerformed (ActionEvent event) { // only listener at this time is text input, so get text and // send it String line = textInput.getText(); if (line==null) line = ""; textInput.setText(""); text.append("\n(" + line + ")"); try { output.write(line,0,line.length()); output.write("\n"); output.flush(); } catch (IOException e) { text.append(e.toString()); } } public void windowOpened(WindowEvent w) {} public void windowClosing(WindowEvent w) { active = false; if (gui) frame.setVisible(false); System.exit(0); } public void windowClosed(WindowEvent w) { } public void windowIconified(WindowEvent w) {} public void windowDeiconified(WindowEvent w) {} public void windowActivated(WindowEvent w) {} public void windowDeactivated(WindowEvent w) {} }