/**** EchoServer.java ****/ /********************************************************************** * Copyright (c) 1991 - 1997 Jerry Smith. * * This software is provided for demonstration purposes only. As * freely-distributed, modifiable source code, this software carries * absolutely no warranty. Jerry Smith disclaims all warranties for * this software, including any implied warranties of merchantability * and fitness, and shall not be liable for damages of any type * resulting from its use. * Permission to use, copy, modify, and distribute this source code * for any purpose and without fee is hereby granted, provided that * the above copyright and this permission notice appear in all * copies and supporting documentation, and provided that Jerry Smith * not be named in advertising or publicity pertaining to the * redistribution of this software without specific, written prior * permission. **********************************************************************/ import java.io.*; import java.net.*; public class EchoServer extends Thread { static final String APP_NAME = "EchoServer"; static final int PORT = 2001; static ServerSocket serverSocket; static int port = PORT; Socket clientSocket; BufferedReader is = null; BufferedWriter os = null; public EchoServer(Socket cs) { clientSocket = cs; } public static void main(String args[]) { if (usageOnly(args)) System.exit(0); initialize(args); while (true) { try { Socket clientSocket = serverSocket.accept(); EchoServer es = new EchoServer(clientSocket); es.start(); } catch (IOException e) { printMsg("Cannot accept client connection."); } } } public void run() { processClientRequest(); } private static boolean usageOnly(String args[]) { if (args.length > 1 || (args.length == 1 && (args[0].equalsIgnoreCase("-usage") || args[0].equalsIgnoreCase("-help") || args[0].equalsIgnoreCase("-h")))) { System.out.println("Usage: java " + APP_NAME + " []"); System.out.println(" The default port is 2001."); return true; } else return false; } private static void initialize(String args[]) { processCommandLine(args); try { serverSocket = new ServerSocket(port); } catch (IOException e) { printMsg("Cannot create server socket " + "on port: " + port + ". Exiting..."); System.exit(0); } } private void processClientRequest() { try { os = new BufferedWriter( new OutputStreamWriter(clientSocket.getOutputStream())); is = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); } catch (IOException e) { printMsg("Cannot handle client connection."); cleanup(); return; } try { String input = is.readLine(); if (input != null) { input = APP_NAME + ": " + input; os.write(input, 0, input.length()); os.flush(); } } catch (IOException e) { printMsg("I/O error while processing client's print file."); } cleanup(); } private void cleanup() { try { if (is != null) is.close(); if (os != null) os.close(); if (clientSocket != null) clientSocket.close(); } catch (IOException e) { printMsg("I/O error while closing connections."); } } private static void processCommandLine(String args[]) { if (args.length != 1) return; port = Integer.parseInt(args[0]); if (port < 1 || port > 65535) { port = PORT; printMsg("Using port " + port + " instead."); } } private static void printMsg(String msg) { System.out.println(APP_NAME + ": " + msg); } }