// Knock KNock Server import java.net.*; import java.io.*; import java.util.*; public class KnockKnockServer { public static void main (String args[]) throws IOException { int port = 1234; ServerSocket server = new ServerSocket(port); int State =1; while (true) { // System.out.println("Waiting for client..."); Socket client = server.accept(); // System.out.println("Client from "+client.getInetAddress()+" connected."); InputStream in = client.getInputStream(); DataInputStream Din = new DataInputStream(in); OutputStream out = client.getOutputStream(); DataOutputStream Dout = new DataOutputStream(out); // byte b[] = new byte[11]; // int num = in.read(b); String input = new String (Din.readUTF()); if (State==1) { System.out.println ("Who's There"); State=2; } else if(State==2) { System.out.println (input + " Who?"); State=3; } else if(State==3) { System.out.println ("that was funny!"); State=1; } } } } ----- //KNock KNock Client import java.net.*; import java.io.*; public class KnockKnockClient { public static void main (String args[]) throws IOException { //the proceswing will to have to take plavce in a contiuous while loop: while (true) { //get a socket on the same port defined in the server; in this case 1234 Socket server = new Socket("localhost",1234); //put an output stream on that socket OutputStream out = server.getOutputStream(); //to let it output stings, put a DtaOutputStream on it DataOutputStream Dout = new DataOutputStream(out); //Interaction with the keyboard: //Make aDataInputReader to read fromstandard input DataInputStream termIn = new DataInputStream(System.in); //ask for input from user System.out.println("Enter next line of joke (press return to finish):"); //read the input and call it "LineOfJoke" String LineOfJoke = new String (termIn.readLine()); //Write it to the sream using the DAtaOutputStream Dout.writeUTF(LineOfJoke); } } }