コード例 #1
0
ファイル: Server.java プロジェクト: naory159/Ex4
  /*
   *  to broadcast a private message to all Clients
   */
  private synchronized void broadcastOnlyOne(String message, ChatMessage cm) {

    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\r\n";
    System.out.print(messageLf);
    serverGUI.serverTextArea.append(messageLf);

    // we loop in reverse order in case we would have to remove a Client
    // because it has disconnected
    for (int i = al.size(); --i >= 0; ) {
      ClientThread ct = al.get(i);
      // try to write to the Client if it fails remove it from the list
      if (cm.getType() == ChatMessage.ESTABLISHCONNECTION
          || cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY) {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(cm)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      } else {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(message)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      }
    }
  }
コード例 #2
0
  public void start() {
    keepGoing = true;
    /* create socket server and wait for connection requests */
    try {
      // the socket used by the server
      ServerSocket serverSocket = new ServerSocket(port);

      // infinite loop to wait for connections
      while (keepGoing) {
        // format message saying we are waiting
        display("Server menunggu koneksi Client pada port " + port + ".");

        Socket socket = serverSocket.accept(); // accept connection
        // if I was asked to stop
        if (!keepGoing) break;
        ClientThread t = new ClientThread(socket); // make a thread of it
        al.add(t); // save it in the ArrayList
        t.start();
      }
      // I was asked to stop
      try {
        serverSocket.close();
        for (int i = 0; i < al.size(); ++i) {
          ClientThread tc = al.get(i);
          try {
            tc.sInput.close();
            tc.sOutput.close();
            tc.socket.close();
          } catch (IOException ioE) {
            // not much I can do
          }
        }
      } catch (Exception e) {
        display("Exception closing the server and clients: " + e);
      }
    }
    // something went bad
    catch (IOException e) {
      String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e + "\n";
      display(msg);
    }
  }
コード例 #3
0
ファイル: Server.java プロジェクト: naory159/Ex4
  /*
   *  to broadcast a message to all Clients
   */
  private synchronized void broadcast(String message) {
    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\n";
    System.out.print(messageLf);
    serverGUI.serverTextArea.append(messageLf);

    // we loop in reverse order in case we would have to remove a Client
    // because it has disconnected
    for (int i = al.size(); --i >= 0; ) {
      ClientThread ct = al.get(i);
      // try to write to the Client if it fails remove it from the list
      if (!ct.writeMsg(messageLf)) {
        al.remove(i);
        display("Disconnected Client " + ct.username + " removed from list.");
      }
    }
  }