1 /* Adapted from an example by 2 * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) 3 */ 4 5 6 import java.io.*; 7 import java.net.*; 8 9 class EchoServer 10 { public static void main(String[] args ) 11 { try 12 { ServerSocket s = new ServerSocket(4882); 13 Socket incoming = s.accept( ); 14 DataInputStream in = new DataInputStream(incoming.getInputStream()); 15 PrintStream out = new PrintStream(incoming.getOutputStream()); 16 17 out.println( "Hello! Enter BYE to exit.\r" ); 18 19 boolean done = false; 20 while (!done) 21 { String str = in.readLine(); 22 if (str == null) done = true; 23 else 24 { out.println("Echo: " + str + "\r"); 25 26 if (str.trim().equals("BYE")) 27 done = true; 28 } 29 30 } 31 32 incoming.close(); 33 } 34 catch (Exception e) 35 { System.out.println(e); 36 } 37 } 38 } 39 40