import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class KnockServer { // port to bind to static int port = 1026; static boolean serverRunning = false; static String[][] JOKES = { {"Harry", "Harry up and answer this door" }, {"Heart", "Heart to hear you, speak louder" }, {"Heaven", "Heaven seen you in ages" }, {"Costa", "Costa lot" }, {"Francie", "Francie that" }, {"Kareem", "Kareem of the crop" }, {"Ken", "Ken I come in or do I have to climb through a window" }, {"Lena", "Lena little closer and I'll tell you" }, {"Tuna", "Tuna piano and it'll sound better" }, }; // standard program entry point public static void main(String args[]) { // The communication attempt must be embedded in a 'try-catch' block // to catch the many errors that could occur during communications try { // Starting server by binding to requested port if possible. ServerSocket server = new ServerSocket(port); System.out.println("Server started: " + server); // Setting up infinite loop to handle multiple client // connections in sequence. serverRunning = true; while (serverRunning) { // Waiting for incoming connection in a b;ocking call Socket socket = server.accept(); System.out.println("Client Connected: " + socket); // Handling the client try { handleClient(socket); } catch(IOException ioe) { System.err.println("Client error: " + ioe.getMessage()); } // We are done so we close the client connection. The server // itself will remain running, waiting for the next client // to connect t the top of the loop. System.out.println("Client Disconnected"); socket.close(); } // Closing down the server. At this time this statement is // unreachable (which should change in improved versions). server.close(); System.out.println("Server Disconnected"); } catch (IOException e) { // Printing error message for all IO exceptions e.printStackTrace(); } } private static void handleClient(Socket socket) throws IOException { // setting timeout socket.setSoTimeout(10000); // setting up IO streams BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter( socket.getOutputStream())); System.out.println("IO Streams established"); String version = in.readLine(); if (version.equals("KK/1.0")) { println(out, "KK OK:" + JOKES.length); String jokeID = in.readLine(); try { int id = Integer.parseInt(jokeID); if ( (0 <= id) && (id < JOKES.length)) { println(out, "KK OK"); println(out, "KK SETUP:" + JOKES[id][0]); println(out, "KK PUNCH:" + JOKES[id][1]); println(out, "KK BYE"); } else println(out, "KK ERROR 100"); } catch(NumberFormatException nfe) { println(out, "KK ERROR 200"); } } else println(out, "KK ERROR 001"); in.close(); out.close(); } private static void println(PrintWriter out, String string) { out.println(string); out.flush(); } }