// This example is from the book _Java in a Nutshell_ by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // Modified by Roger Webster, Ph.D. import java.io.*; import java.net.*; public class rogerserver extends Thread { public final static int DEFAULT_PORT = 7777; protected int port; protected ServerSocket listen_socket; int N=0; public static void fail(Exception e, String msg) { System.err.println(msg + ": " + e); System.exit(1); } // Create ServerSocket to listen for connections start the thread. public rogerserver (int port) { if (port == 0) port = DEFAULT_PORT; this.port = port; try { listen_socket = new ServerSocket(port); } catch (IOException e) { fail(e, "Exception creating server socket");} System.out.println("Server: listening on port " + port); this.start(); } // The body of the server thread. Loop forever, listening for and // accepting connections from clients. For each connection, // create a Connection object to handle communication through the // new Socket. public void run() { try { while(true) { Socket client_socket = listen_socket.accept(); Connection c = new Connection(client_socket, N); } } catch (IOException e) {fail(e, "Exception while listening for connections");} } // Start the server up, listening on an optionally specified port public static void main(String[] args) { int port = 0; if (args.length == 1) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) {port = 0;} } new rogerserver(port); } } // This class is the thread that handles all communication with a client class Connection extends Thread { protected Socket client; protected DataInputStream in; protected PrintStream out; int myN; // Initialize the streams and start the thread public Connection(Socket client_socket, int N) { client = client_socket; myN = N; try { in = new DataInputStream(client.getInputStream()); out = new PrintStream(client.getOutputStream()); } catch (IOException e) { try {client.close(); } catch (IOException e2) {System.out.println("Exception while getting socket streams: " );} System.out.println("Exception while getting socket streams: "); return; } this.start(); } // Provide the service. // Read a line, reverse it, send it back. public void run() { String line; StringBuffer revline; int len; try { for(;;) { // read in a line line = in.readLine(); if (line == null) break; // reverse it len = line.length(); revline = new StringBuffer(len); for(int i = len-1; i >= 0; i--) revline.insert(len-1-i, line.charAt(i)); // and write out the reversed line out.println(revline); } } catch (IOException e) {System.out.println(e.toString());} finally { try { client.close(); } catch (IOException e2) { System.out.println(e2.toString()); } } } }