/** * a network server that detects presence of dogs on the Internet * * @version 1.1 Jan 21 1996 * @author Peter van der Linden * @author From the book "Just Java" * * */ import java.io.*; import java.net.*; import java.util.Random; class dogserver { public static void main(String a[]) { int port = 4444; if (a.length >0) { port = Integer.parseInt(a[0]); } Socket sock= null; ServerSocket servsock = null; String query = "If you met me would you shake my hand, or sniff it?"; try { servsock = new ServerSocket(port); } catch(IOException e) { System.out.println("Could not open port."); System.exit(1); } while (true) try { // wait for the next client connection sock=servsock.accept(); // Get I/O streams from the socket PrintStream out = new PrintStream( sock.getOutputStream() ); BufferedReader in = new BufferedReader(new InputStreamReader( sock.getInputStream() )); // Send our query out.println(query); out.flush(); // get the reply Random ran = new Random(); String reply = in.readLine(); if (reply != null && reply.toLowerCase().indexOf("sniff") > -1) { //System.out.println("On the Internet I know this is a DOG!"); out.println("You're"+ dogQualifiers[Math.abs(ran.nextInt()) % dogQualifiers.length]+ "a dog."); out.flush(); } else { //System.out.println("Probably a person or an AI experiment"); out.println("You're a person" + peopleQualifiers[Math.abs(ran.nextInt()) % peopleQualifiers.length]); out.flush(); } // Close this connection, (not the overall server socket) sock.close(); } catch (IOException e) { System.out.println("IOException from dog server."); } } private static final String [] dogQualifiers = { " definitely ", " certainly ", " unquestionably ", " without any doubt " }; private static final String [] peopleQualifiers = { " or something.", " or an AI experiment.", " or a very tricky fido.", " of course." }; }