2013-04-08 56 views
1

我開發了一個使用Java的客戶端/服務器聊天應用程序,我希望知道如何從數組中刪除用戶。當特定客戶端登錄時,用戶名稱中的用戶名數組和客戶端ID保存在客戶端陣列中。爲了允許服務器接受多個客戶端,我正在使用線程。現在任何人都可以指導我如何從數組中刪除用戶,並關閉該用戶的連接。從聊天中刪除用戶(Java)

添加一個新的客戶端和保存ID在客戶端陣列

public class AddClient implements Runnable { 

    Thread t; 

    AddClient(String tot) { 
     t = new Thread(this, tot); 
     t.start(); 
    } 

    public void run() { 
     while (true) { 
      try { 
       try { 
        waitClient(); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 

       } 

       for (int i = 0; i < client.length; i++) { 
        if (client[i] == 0) { 
         client[i] = i + 1; 
         id = i; 
         break; 
        } 
       } 

       //set stream to send and receive data 
       out[client[id]] = new ObjectOutputStream(connect.getOutputStream()); 
       out[client[id]].flush(); 
       in[client[id]] = new ObjectInputStream(connect.getInputStream()); 

的用戶名被保存的用戶名陣列

username[client[id]] = cm.sender; //Add user in username[] array 

刪除用戶

public synchronized void removeUser(int number) { 
    int position = number; 
    System.out.println("Server removing user " + username[number] + "which is client " + number); 

    for (int i = 0; i <= client.length; i++) { 
     if (position == client[i]) { 
      System.out.println("User to be remove found"); 
      try { 
       client[i + 1] = client[i]; 
       in[position].close(); 
       out[position].close(); 
       username[position] = null; 
       position = position - 1; 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

我想在使用HashTable添加和刪除客戶端

public class ChatServerProtocol { 
private String nick; 
private AddClient a; 

private Hashtable<String, AddClient> nicks = new Hashtable<String, AddClient>(); 


private boolean add_nick(String nick, AddClient a) { 
    if (nicks.containsKey(nick)) { 
     return false; 
    } else { 
     nicks.put(nick, a); 
     return true; 
    } 
} 

private boolean remove_nick(String nick, AddClient a) { 
    if (!(nicks.containsKey(nick))) { 
     return false; 
    } else { 
     nicks.remove(nick); 
     return true; 
    } 
} 

public ChatServerProtocol(AddClient a) throws IOException { 
    nick = null; 
    a = a; 
} 

但現在我該如何調用方法add_nick。無論何時客戶端登錄用戶名被髮送到服務器,並且服務器將其作爲cm.sender讀取。我還需要包含線程變量。所以如何添加用戶名,以便以後我可以刪除它。

ChatServerProtocol.add_nick(cm.sender); 
+0

你應該指明異常在問題的代碼拋出的線。 – hyde 2013-04-08 10:50:00

+0

另外,不要將Java數組用於這種目的......在這種情況下,您應該使用'Map'。另外,當你感覺像使用數組時,你應該幾乎總是使用'ArrayList'來代替。 – hyde 2013-04-08 10:51:26

+0

我在想,如果我將客戶端ID和用戶名詳細信息發送到我的數據庫,並且只要他們註銷,我只需使用SQL Query刪除他們的詳細信息。這是個好主意嗎? – user2122925 2013-04-08 11:02:58

回答

0

沒有,節省數據庫將不會是一個好主意..記住,你要保存的細節僅用於會話和數據庫的基本概念的長度是在會議結束後使用它。如果因爲網絡問題等原因而中斷會話,會發生什麼情況?

只需使用Map而不是普通的數組,使用鍵作爲客戶端ID和值作爲用戶名。刪除用戶名將是普通的調用,如map.remove(clientID);

編輯爲你問:注意,該代碼是不完整的,只有當你給儘可能多的..

公共類了addClient實現Runnable { 線程t;

private Map<int, String> users = new HashMap <int, String>(); 

AddClient(String tot) { 
    t = new Thread(this, tot); 
    t.start(); 
} 

public void run() { 
    while (true) { 
     try { 
      try { 
       waitClient(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 

      } 

      int clientId = users.size() + 1; 
      users.put(clientId, cm.sender); 


      //set stream to send and receive data 
      out[clientId] = new ObjectOutputStream(connect.getOutputStream()); 
      out[clientId].flush(); 
      in[clientId] = new ObjectInputStream(connect.getInputStream()); 

刪除用戶的方法

市民同步無效removeUser(INT數){

  if(users.containsKey(number)) { 
       System.out.println("Server removing user " + users.get(number) + "which is client " + number); 
       users.remove(number); 
      } else { 
       System.out.println("User not in session"); 
      } 
} 
+0

好的,謝謝!現在會自己嘗試。 – user2122925 2013-04-08 11:18:41

+0

我編輯了我的問題!我很抱歉,但這是我第一次使用HashTable。所以你能幫我解決這個問題。謝謝。 – user2122925 2013-04-08 12:39:56