// Purpose. MasterMind server derived class import java.io.*; import java.net.*; public class MasterMindServer extends ServerBaseClass { private int answer[] = new int[4]; private int answerNums[] = new int[7]; public MasterMindServer() { for (int i=0; i < 4; i++) answer[i] = (int) (Math.random()*1000) % 6 + 1; for (int i=0; i < 4; i++) answerNums[ answer[i] ]++; System.out.print( "MasterMindServer: " ); for (int i=0; i < 4; i++) System.out.print( answer[i] + " " ); System.out.println(); } public void run( Socket s ) { try { System.out.println("MasterMindServer.run(Socket): entry"); DataInputStream inn = new DataInputStream( s.getInputStream() ); DataOutputStream out = new DataOutputStream( s.getOutputStream() ); int[] input = new int[4]; int[] inputNums = new int[7]; int[] bw = new int[2]; while (true) { for (int i=0; i < 7; i++) inputNums[i] = 0; for (int i=0; i < 2; i++) bw[i] = 0; for (int i=0; i < 4; i++) input[i] = inn.readInt(); for (int i=0; i < 4; i++) inputNums[ input[i] ]++; for (int i=0; i < 4; i++) if (input[i] == answer[i]) bw[0]++; for (int i=1; i < 7; i++) bw[1] += (inputNums[i] < answerNums[i]) ? inputNums[i] : answerNums[i]; bw[1] = bw[1] - bw[0]; for (int i=0; i < 2; i++) out.writeInt( bw[i] ); } } catch( IOException e ) { e.printStackTrace(); } } public static void main( String[] args ) { MasterMindServer server = new MasterMindServer(); server.startServer( 8189 ); } } //////// master thread/server initializing // MasterMindServer: 1 5 4 3 // startServer: entry // startServer: before runner.start() // ServerBaseClass.run(): entry //////// a MasterMindClient creates a Socket( "127.0.0.1", 8189 ) // before newSocket.runner.start() // ServerBaseClass.run(): entry // MasterMindServer.run(Socket): entry //////// another MasterMindClient creates a Socket( "127.0.0.1", 8189 ) // before newSocket.runner.start() // ServerBaseClass.run(): entry // MasterMindServer.run(Socket): entry //////// a MasterMindClient solves the puzzle and exits // java.net.SocketException: Connection reset by peer: // at java.io.DataInputStream.readInt(DataInputStream.java:369) // at MasterMindServer.run(MasterMindServer.java:71) // at ServerBaseClass.run(MasterMindServer.java:42) // at java.lang.Thread.run(Thread.java:490) // Purpose. MasterMind client import java.io.*; import java.net.*; class MasterMindClient { public static void main( String[] args ) { try { Socket s = new Socket( "127.0.0.1", 8189 ); DataInputStream inn = new DataInputStream( s.getInputStream() ); DataOutputStream out = new DataOutputStream( s.getOutputStream() ); while (true) { int num[] = new int[4]; int bw[] = new int[2]; // prompt for input from the user System.out.print( "Input: " ); // read 4 integers from standard-in for (int i=0; i < 4; i++) num[i] = Read.cinInt(); // pass the user input to the MasterMindServer via the socket for (int i=0; i < 4; i++) out.writeInt( num[i] ); // retrieve the response from the MasterMindServer via the socket for (int i=0; i < 2; i++) bw[i] = inn.readInt(); // forward the response to standard-out System.out.println( " " + bw[0] + " " + bw[1] ); // if the puzzle is solved, exit if (bw[0] == 4) break; } s.close(); } catch( IOException e ) { e.printStackTrace(); } } } // Input: 1 1 2 2 // 1 0 // Input: 3 3 4 4 // 1 1 // Input: 3 4 2 5 // 0 3 // Input: 4 3 6 2 // 0 2 // Input: 1 5 4 3 // 4 0