/* Adapted from an example by * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) */ import java.io.*; import java.net.*; class EchoServer { public static void main(String[] args ) { try { ServerSocket s = new ServerSocket(4882); Socket incoming = s.accept( ); 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; } } incoming.close(); } catch (Exception e) { System.out.println(e); } } }