/** * A separate thread that is created by the server. This thread is used * to interact with the client. */ import java.io.*; import java.net.*; public class Connection extends Thread { public Connection(Socket c) { client = c; } /** * this method is invoked as a separate thread */ public void run() { BufferedReader networkBin = null; OutputStreamWriter networkPout = null; try { /** * get the input and output streams associated with the socket. */ networkBin = new BufferedReader(new InputStreamReader(client.getInputStream())); networkPout = new OutputStreamWriter(client.getOutputStream()); /** * the following successively reads from the input stream and returns * what was read. The loop terminates with ^D or with the string "bye\r\n" * from the input stream. */ while (true) { String line = networkBin.readLine(); if ( (line == null) || line.equals("bye")) { break; } networkPout.write("Server [ "+line+" ]\r\n"); networkPout.flush(); } } catch (IOException ioe) { System.err.println(ioe); } finally { try { if (networkBin != null) networkBin.close(); if (networkPout != null) networkPout.close(); if (client != null) client.close(); } catch (IOException ioee) { System.err.println(ioee); } } } private Socket client; }