import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /* * Created on Feb 2, 2005 * * Try altering this so it doesn't keep asking about your mother after the first time * Have it clense words out of a sentence. * Have it reverse the letters of a word. */ /** * @author dano * * Simple server accepts a single socket connection, processed incoming text and spits text back * */ public class SuperSimpleServer { //this server accepts one connection at a time. // you can connect to it from telnet public static void main(String[] args) { ServerSocket frontDoor; Socket connection; String textSoFar = ""; InputStream in; OutputStream out; int portNum = 9001; try { frontDoor = new ServerSocket(portNum); System.out.println("Set up a door at" + frontDoor.getInetAddress()+ " " + frontDoor.getLocalPort()); while (true) { System.out.println("Waiting for a new connection"); connection = frontDoor.accept();//sits here and waits for // someone to knock on the door System.out.println(connection.getRemoteSocketAddress() + " knocked on the door and I let them in"); in = connection.getInputStream(); out = connection.getOutputStream(); while (true) { int input = in.read(); if (input == -1) { System.out.println("Connection is broken"); break; } textSoFar = textSoFar + (char) input; if (input == 13) { //carriage return if (textSoFar.indexOf("mother") != -1) { String outputString = "How do you feel about your mother? "; out.write(outputString.getBytes()); } else { String outputString = "Interesting, tell me more. "; out.write(outputString.getBytes()); } } } } } catch (IOException e) { System.out.println("Had a problem making a front door" + e); } } }