Ejemplo n.º 1
0
 public void leave() {
   try {
     // int id = ChatServer.getClientID(name);
     // ChatServer.sendAll(CS + "e" + name + " has left the ChatServer");
     ChatServer.sendOne(clientID, "kick");
     ChatServer.removeClient(clientID);
     ChatServer.updateUsers();
     socket.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  /**
   * close: close the socket and stream
   *
   * @return: true for success, false for error
   */
  protected boolean close() {
    _dlog("Do main thread closing...");
    if (_sock != null && !_sock.isClosed()) {
      _dlog("Closing the receiving thread");
      try {
        _sock.close();
        _in.close();
        _out.close();

        _sock = null;
        _in = null;
        _out = null;
        _dlog("Success!");
      } catch (IOException e) {
        if (!_server.noException()) {
          _elog(e.toString());
        }
        if (_server.debugMode()) {
          e.printStackTrace();
        }
        return false;
      }
    }
    _sock = null;
    _in = null;
    _out = null;

    _dlog("Cancel the user input thread");
    if (_userNet != null) {
      _userNet.stop();
      _userNet.join(); // guaranteed to be closed
      _userNet = null;
    }
    if (_userSock != null && !_userSock.isClosed()) {
      _dlog("Closing the user input thread");
      // Stop the user thread
      try {
        _userSock.close();
        _userIn.close();
        _userOut.close();
        _userSock = null;
        _userIn = null;
        _userOut = null;
        _dlog("Success!");
      } catch (IOException e) {
        if (!_server.noException()) {
          _elog(e.toString());
        }
        if (_server.debugMode()) {
          e.printStackTrace();
        }
        return false;
      }
    }
    _userSock = null;
    _userIn = null;
    _userOut = null;

    /* Cancel all client nodes */

    _dlog("Finished");
    return true;

    /* CAUTION: _server is never cleared */
  }
Ejemplo n.º 3
0
 public void handleMessageFromClientUI(Object message) {
   String parsed = "";
   if (!((Message) message).message.startsWith("#") && isConnected()) {
     super.handleMessageFromClientUI(message);
     return;
   }
   {
     parsed = ((Message) message).message.substring(1);
     if (parsed.equals("quit")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
       System.exit(0);
     } else if (parsed.equals("logoff")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
     } else if (parsed.equals("login")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       try {
         openConnection();
       } catch (IOException ex) {
         System.out.println("Error: " + ex.getMessage());
         ex.printStackTrace();
       }
     } else if (parsed.startsWith("sethost")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       } else {
         String host = "";
         try {
           host = parsed.split(" ")[1];
         } catch (ArrayIndexOutOfBoundsException ex) {
           System.out.println("Error: Must specify host");
           return;
         }
         setHost(host);
       }
     } else if (parsed.startsWith("setport")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       int port = 0;
       try {
         port = Integer.parseInt(parsed.split(" ")[1]);
       } catch (ArrayIndexOutOfBoundsException ex) {
         System.out.println("Error: Must specify port");
         return;
       }
       setPort(port);
     } else if (parsed.equals("getport")) {
       System.out.println("Port: " + getPort());
     } else if (parsed.equals("gethost")) {
       System.out.println("Host: " + getHost());
     }
   }
 }
  /**
   * connect: connect to the master
   *
   * @return: true for success and false for failure
   */
  protected boolean connect() {

    boolean connected = true;
    try {
      _sock = new Socket(_serverIP, _serverPort);
      _out = new PrintWriter(_sock.getOutputStream(), true);
      _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
      String reply = sendAll();
      if (!reply.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        /* Tokenize */
        StringTokenizer st = new StringTokenizer(reply);
        if (st.nextToken().equals(ProtocolConstants.PACK_STR_ERRMES_HEAD)) {
          _elog(st.nextToken());
          return false;
        }
      }

      _userSock = new Socket(_serverIP, _serverPort);
      _userOut = new PrintWriter(_userSock.getOutputStream(), true);
      _userIn = new BufferedReader(new InputStreamReader(_userSock.getInputStream()));
      String replyUser = sendUserID();
      if (!replyUser.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        throw new Exception("Not confirmed");
      }
    } catch (UnknownHostException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (IOException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (Exception e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    }
    if (!connected) {
      return connected;
    }

    if (!connected) {
      /* Remove the pair of socket */
      _sock = null;
      _in = null;
      _out = null;

      _userSock = null;
      _userOut = null;
      _userIn = null;
    }
    return connected;
  }