import java.net.*; import java.io.*; /* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved. A simple server program. It needs the class 'JokeHandler' that contains the protocol that the server understands. The code is taken from the excellent SUN online Java tutorial and slightly modified. */ class JokeServer { public static void main(String args[]) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: " + 4444 + ", " + e); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: " + 4444 + ", " + e); System.exit(1); } try { DataInputStream inStream = new DataInputStream( new BufferedInputStream(clientSocket.getInputStream())); PrintStream outStream = new PrintStream( new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false); JokeHandler handler = new JokeHandler(); String inputLine, outputLine; outputLine = handler.processInput(null); outStream.println(outputLine); outStream.flush(); while ((inputLine = inStream.readLine()) != null) { outputLine = handler.processInput(inputLine); outStream.println(outputLine); outStream.flush(); if (outputLine.equals("Bye")) break; } outStream.close(); inStream.close(); clientSocket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }