コード例 #1
0
  // This method is called when a request has been made to close a client connection
  public synchronized void disconnectClient(ClientConnection clientConnection) {

    // Ensures this connection exists (a necessary check when this is being called from outside of a
    // clientConnection)
    if (clientConnection != null
        && clientConnection.isConnected()
        && clientSocketList.get(clientSocketList.indexOf(clientConnection)) != null) {

      // Send the client a message telling it to close its socket
      clientConnection.xmitMessage(ClientConnection.QUIT);

      // Then close that socket...the user really doesn't have a choice.  We're just asking to be
      // nice.
      clientSocketList.get(clientSocketList.indexOf(clientConnection)).closeSocket();
      parent.updateStatusBox(
          clientSocketList.indexOf(clientConnection)
              + " "
              + "Forcing disconnection of client "
              + clientConnection.id
              + " on port "
              + port
              + "...");
    } else {

      // If this thread is NOT connected, it may be stuck, so let's send an interrupt to wake it up
      clientThreadList.get(clientSocketList.indexOf(clientConnection)).interrupt();
      parent.updateStatusBox("Closing listen socket on port " + port + "...");
    }
  }
コード例 #2
0
  // THIS IS WHERE THE MAGIC HAPPENS -- on the server side at least
  // This method sends some given message to ALL connected clients by creating an iterator from
  // clientSocketList
  //      and executing the xmitMessage method for each element in that iterator
  public synchronized void broadcastMsg(String message) {

    Iterator<ClientConnection> clientIterator = clientSocketList.iterator();
    ClientConnection clientConnection;
    while (clientIterator.hasNext()) {
      clientConnection = clientIterator.next();
      clientConnection.xmitMessage(message);
    }
  }
コード例 #3
0
  // This method is called as a part of the client handshake -- the client makes a request to join
  // the server
  //      and this method runs to check for information that should be sent back before they're
  // ready to operate
  // NOTE: In the original chat server implementation, this also sent the client information about
  // the connected
  //      users and that kind of thing -- a handshake method like this is helpful when you want to
  // provide some
  //      kind of immediate functionality to users who connect in the middle of something already in
  // progress
  //      (like a vote or chat already in progress)
  public synchronized void clientJoined(ClientConnection clientConnection) {

    // If voting is already in progress, send the user the question list and allow them to start
    // voting
    if (voting) {

      clientConnection.xmitMessage("/numq" + numQuestions);

      for (int x = 0; x <= (numQuestions - 1); x++) {
        clientConnection.xmitMessage("/ques" + String.valueOf(x) + questions[x]);
      }

      clientConnection.xmitMessage("/stvt");
    } else {
      // As a part of the handshake, the client is expecting some form of reply.  This is
      // placeholder for
      //      something actually useful
      clientConnection.xmitMessage("You're connected!");
    }
  }