magpie:network/> cat -n Server.java 1 // This example is from the book _Java in a Nutshell_ by David Flanagan. 2 // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. 3 // You may study, use, modify, and distribute this example for any purpose. 4 // This example is provided WITHOUT WARRANTY either expressed or implied. 5 // Modified by tsaiwn@csie.nctu.edu.tw with BufferedReader to read data 6 // .. plus, handling EOF / disconnection request 7 8 import java.io.*; 9 import java.net.*; 10 public class Server extends Thread { 11 public final static int DEFAULT_PORT = 6789; 12 protected int port; 13 protected ServerSocket listen_socket; 14 15 // Exit with an error message, when an exception occurs. 16 public static void fail(Exception e, String msg) { 17 System.err.println(msg + ": " + e ); 18 System.exit(1); 19 } 20 21 // Create a ServerSocket to listen for connections on; start the thread. 22 public Server(int port) { // constructor 23 if (port == 0) port = DEFAULT_PORT; 24 this.port = port; 25 try { listen_socket = new ServerSocket(port); } 26 catch (IOException e) { 27 fail(e, "Exception creating server socket"); 28 } 29 System.out.println("Server: listening on port " + port); 30 this.start(); 31 } // Server constructor 32 33 // The body of the server thread. Loop forever, listening for and 34 // accepting connections from clients. For each connection, 35 // create a Connection object to handle communication through the 36 // new Socket. 37 public void run() { 38 try { 39 while(true) { // accept( ) will wait someone to connect me 40 Socket client_socket = listen_socket.accept(); 41 Connection c = new Connection(client_socket); // thread 42 } // while(true); Loop forever to serve clients 43 } 44 catch (IOException e) { 45 fail(e, "Exception while listening for connections"); 46 } 47 } // run 48 49 // Start the server up, listening on an optionally specified port 50 public static void main(String[ ] args) { 51 int port = 0; 52 if (args.length == 1) { 53 try { port = Integer.parseInt(args[0]); } 54 catch (NumberFormatException e) { port = 0; } 55 } 56 new Server(port); 57 } // main 58 } // class Server ends here 59 60 // This class is the thread that handles all communication with a client 61 class Connection extends Thread { 62 protected Socket client; 63 protected DataInputStream dis; 64 protected InputStreamReader isr; 65 protected BufferedReader in; 66 protected PrintStream out; 67 protected InetAddress who; // Client IP_address 68 protected String name; // Client Host_Name 69 70 // Initialize the streams and start the thread 71 public Connection(Socket client_socket) { // constructor 72 client = client_socket; 73 who = client.getInetAddress( ); // by tsaiwn 74 name = " " + who.getHostName( ); 75 java.util.Date now = new java.util.Date( ); 76 System.err.println(""+who+name+" entered at " + now); // by tsaiwn 77 try { 78 dis = new DataInputStream(client.getInputStream()); 79 isr = new InputStreamReader(dis); 80 in = new BufferedReader(isr); // by tsaiwn@csie.nctu.edu.tw 81 out = new PrintStream(client.getOutputStream()); 82 } 83 catch (IOException e) { 84 try { client.close(); } catch (IOException e2) { ; } 85 System.err.println("Exception while getting socket streams: " + e); 86 return; 87 } 88 this.start(); 89 } // Connection constructor 90 91 // Provide the service. 92 // Read a line, reverse it, send it back. 93 public void run() { 94 String line; 95 StringBuffer revline; 96 int len; 97 try { 98 for(;;) { // loop forever 99 // read in a line 100 try { 101 line = in.readLine(); 102 } catch (EOFException e) { line = null; } 103 if (line == null) break; 104 System.err.println(""+line.length()+" "+line); // tsaiwn 105 // reverse it 106 len = line.length(); 107 if(len==1) if( (int)(line.charAt(0)) == 3 ) break; // CTRL_C 108 if(len==1) if( (int)(line.charAt(0)) == 26 ) break; // CTRL_Z 109 revline = new StringBuffer(len); 110 for(int i = len-1; i >= 0; i--) 111 revline.insert(len-1-i, line.charAt(i)); 112 // and write out the reversed line 113 out.println(revline); 114 } 115 } catch (IOException e) { ; } 116 finally { try {client.close();} catch (IOException e2) {;} 117 java.util.Date now = new java.util.Date( ); 118 System.err.println(""+who+name+" leaving at " + now); 119 } 120 } // run 121 } // class Connection 122