import java.io.*; import java.net.*; class CheckersServer { //Array of threads CheckersServerThread[] cstList = new CheckersServerThread[2]; public static void main(String[] args) { new CheckersServer().start(); } public void start() { ServerSocket listenSocket = null; //Keeps track of the number of clients connected. int numberConnected = 0; try { //Locked into listening on port 5000. listenSocket = new ServerSocket(5000); System.out.println("CheckersServer listening on port 5000."); } catch (IOException e) { System.err.println("CheckersServer failed to open port 5000."); return; } while (true) { while (numberConnected < 2) { //Connects clients to server, until 2 people are connected. CheckersServerThread cst; cst = connectToClient(listenSocket); if (cst != null) { numberConnected++; if (cstList[0] == null) { cstList[0] = cst; System.out.println("player1 connected"); writeToStream("player1", cstList[0].outputStream); } else { cstList[1] = cst; System.out.println("player2 connected"); writeToStream("player2", cstList[1].outputStream); } } } if (!everythingIsOK(0) || !everythingIsOK(1)) { System.err.println("Two (2) Threads created, but errors encountered."); } while (numberConnected == 2 || numberConnected == 1) { //Deals with disconnections. if (!everythingIsOK(0)) { numberConnected--; System.out.println("numberConnected: " + numberConnected); cleanup(cstList[0]); cstList[0] = null; } if (!everythingIsOK(1)) { numberConnected--; System.out.println("numberConnected: " + numberConnected); cleanup(cstList[1]); cstList[1] = null; } if (numberConnected == 0) break; try { Thread.sleep(1000); } catch(InterruptedException e) { //Do nothing } } } } protected CheckersServerThread connectToClient(ServerSocket listenSocket) { //Makes connections to clients. Socket rendezvousSocket = null; CheckersServerThread cst = null; //Try and create a data socket. try { rendezvousSocket = listenSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); return null; } try { //Create a new server thread. cst = new CheckersServerThread(rendezvousSocket, this); cst.start(); } catch (Exception e) { System.err.println("Error when creating CheckersServerThread."); return null; } return cst; } boolean everythingIsOK(int cstNum) { CheckersServerThread cst = cstList[cstNum]; //Checks for any errors in the chat threads. if (cst == null) { return false; } else { if (cst.outputStream == null) { return false; } if (cst.inputStream == null) { return false; } if (cst.socket == null) { return false; } } return true; } void cleanup(CheckersServerThread cst) { //Closes all I/O streams and the socket ignoring all errors. if (cst != null) { try { if (cst.outputStream != null) { cst.outputStream.close(); cst.outputStream = null; } } catch (Exception e) {} try { if (cst.inputStream != null) { cst.inputStream.close(); cst.inputStream = null; } } catch (Exception e) {} try { if (cst.socket != null) { cst.socket.close(); cst.socket = null; } } catch (Exception e) {} } } public void forwardString(String string, CheckersServerThread requestor) { //Takes a string from one client and sends it to the other. int i; BufferedWriter clientStream = null; if (string.equals("disconnect")) { for (i = 0; i < 2; i++) { writeToStream("disconnect", cstList[i].outputStream); cstList[i] = null; } } if (cstList[0] == requestor) { if (cstList[1] != null) { clientStream = cstList[1].outputStream; } else { return; } } else { if (cstList[0] != null) { clientStream = cstList[0].outputStream; } else { return; } } if (clientStream != null) { writeToStream(string, clientStream); } } public void writeToStream(String string, BufferedWriter stream) { //Write the string to the data stream. try { stream.write(string); stream.newLine(); stream.flush(); } catch (IOException e) { System.err.println("Could not forward message." + string); return; } catch (NullPointerException e) { System.err.println("outputStream is null."); return; } } }