import java.io.*; import java.net.*; /** * Esta clase implementa al servidor del juego KaBoom, su misión es coordinar * el juego (establecer los turnos de juego), recibir los comandos provenientes * desde un programa cliente y replicarlo al resto de los jugadores (tipo echo * server). */ public class KaServer { /** * Puerto que es atendido. */ private static int port = 10000; public static void main(String[] args) { /* socket cliente 1 */ Socket cs1; BufferedReader in1; PrintWriter out1; /* socket cliente 1 */ Socket cs2; BufferedReader in2; PrintWriter out2; try { int i = 1; ServerSocket s = new ServerSocket(port); System.out.println(s.getInetAddress().getHostAddress() + " : " + port); /* cliente 1 se conecta */ cs1 = s.accept( ); System.out.println("1 clientes conectados"); in1 = new BufferedReader(new InputStreamReader(cs1.getInputStream())); out1 = new PrintWriter(cs1.getOutputStream(), true /* autoFlush */); out1.println("Bienvenido a KaServer"); out1.println("1"); /* cliente 2 se conecta */ cs2 = s.accept( ); System.out.println("2 clientes conectados"); in2 = new BufferedReader(new InputStreamReader(cs2.getInputStream())); out2 = new PrintWriter(cs2.getOutputStream(), true /* autoFlush */); out2.println("Bienvenido a KaServer"); out2.println("2"); System.out.println("Iniciando juego"); out1.println("END_MAP"); out2.println("END_MAP"); out1.println("END_WARRIOR"); out2.println("END_WARRIOR"); /* le toca a jugador 1 */ out1.println("1"); out2.println("1"); int turn = 1; String str = null; BufferedReader in = null; PrintWriter out = null; String cmd = null; do { if (turn == 1) { str = "1"; in = in1; out = out2; } else { str = "2"; in = in2; out = out1; } cmd = in.readLine(); System.out.println("client " + str + ": " + cmd); out.println(cmd); if (cmd.equals("END_TURN")) { if (turn == 1) turn = 2; else turn =1; continue; } if (cmd.equals("END_GAME")) System.exit(0); }while (!cmd.equals("END_GAME")); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } }