// EchoClient.java import java.io.*; import java.net.*; class EchoClient { public static void main(String[] args) throws IOException { Socket t = new Socket(args[0], 8189); //8189 is chosen by the programmer String str; DataInputStream is = new DataInputStream(t.getInputStream()); PrintStream out = new PrintStream(t.getOutputStream()); DataInputStream in = new DataInputStream(System.in); System.out.println("Hello! Enter BYE to exit.\r"); boolean more = true; while (more) { System.out.println("type in a line : "); str = in.readLine(); if ((str != null) && !(str.trim().equals("BYE"))) { out.println(str); str = is.readLine(); System.out.println(str); } else more = false; } // while } // main } // ThreadEchoServer.java import java.io.*; import java.net.*; public class ThreadEchoServer { public static void main(String[] args) { int i = 0; try { // port 8189 is chosen by the programmer ServerSocket s = new ServerSocket(8189); for (;;) { Socket incoming = s.accept(); i ++; System.out.println("Spawning " + i); ThreadEchoHandler t = new ThreadEchoHandler(incoming, i); t.start(); } } catch (Exception e) { System.out.println(e); } } // main } // ThreadEchoServer // ThreadEchoHandler.java import java.io.*; import java.net.*; public class ThreadEchoHandler extends Thread { public ThreadEchoHandler(Socket i, int c) { incoming = i; counter = c; } public void run() { try { DataInputStream in = new DataInputStream(incoming.getInputStream()); PrintStream out = new PrintStream(incoming.getOutputStream()); // out.println("Hello! Enter BYE to exit.\r"); boolean done = false; while (!done) { String str = in.readLine(); if (str == null) done = true; else { out.println("Echo: " + str + "\r"); if (str.trim().equals("BYE")) done = true; } } // while incoming.close(); } // try catch (Exception e) { System.out.println(e); } } // run private Socket incoming; private int counter; }