Ejemplo n.º 1
0
  @Override
  public void run() {
    System.out.println("Starting Listener");
    int character;
    try {
      socket1 = new ServerSocket(port);
      game.gui.lock("Waiting for other player");
      connection = socket1.accept();

      bis = new BufferedInputStream(connection.getInputStream());
      isr = new InputStreamReader(bis);

      while ((character = isr.read()) != 13) {
        strb.append((char) character);
      }

      // if received CLOSEMESSAGE -> end listening and do something
      if (strb.toString().equals(Online.CLOSEMESSAGE)) {
        System.out.println("Looks like we have ended...");
      }

      // check incoming message
      System.out.println(strb);

      Figure f = game.parseFigure(strb.toString());
      Position p = game.parsePosition(strb.toString());
      game.playerPlay(f, p);

      game.switchPlayers();
      game.gui.unlock();
      connection.close();
      System.out.println("moved, your turn!");
      socket1.close();

    } catch (IOException e) {
      System.out.println("Unexpected exception on server side" + e);
    }
  }