Ejemplo n.º 1
0
  public void run() {
    try {
      try {
        scanner = new Scanner(socket.getInputStream());
        out = new PrintWriter(socket.getOutputStream());

        while (true) {
          checkConn();

          if (!scanner.hasNext()) {
            return;
          }

          msg = scanner.nextLine();

          System.out.println("Client said: " + msg);

          for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
            Socket tSocket = (Socket) Server.ConnectionArray.get(i - 1);
            PrintWriter tOut = new PrintWriter(tSocket.getOutputStream());
            tOut.println(msg);
            tOut.flush();
            System.out.println("Sent to: " + tSocket.getLocalAddress().getHostName());
          }
        }
      } finally {
        socket.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  public void checkConn() {
    try {

      if (!socket.isConnected()) {
        for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
          if (Server.ConnectionArray.get(i) == socket) {
            Server.ConnectionArray.remove(i);
          }
        }
        for (int i = 1; i <= Server.ConnectionArray.size(); i++) {
          Socket tSocket = (Socket) Server.ConnectionArray.get(i - 1);
          PrintWriter tOut = new PrintWriter(tSocket.getOutputStream());
          tOut.println(tSocket.getLocalAddress().getHostName() + " disconnected!");
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }