2012-05-11 50 views
0

我做了一個簡單的聊天應用程序,使用該線程連接服務器和客戶端。我想發送消息給所有活動客戶端。 如何發送消息到活動線程列表? 我使用flush()方法,但未能將消息發送到所有活動的客戶端 我發現在谷歌顯示線程列表的方法如下:如何將消息發送到活動線程列表?

public static void listThreads(ThreadGroup group, String indent) { 
    System.out.println(indent + "Group[" + group.getName() + 
        ":" + group.getClass()+"]"); 
    int nt = group.activeCount(); 
    Thread[] threads = new Thread[nt*2 + 10]; //nt is not accurate 
    nt = group.enumerate(threads, false); 

    // List every thread in the group 
    for (int i=0; i<nt; i++) { 
     Thread t = threads[i]; 
     System.out.println(indent + " Thread[" + t.getName() 
        + ":" + t.getClass() + "]"); 
    } 

    // Recursively list all subgroups 
    int ng = group.activeGroupCount(); 
    ThreadGroup[] groups = new ThreadGroup[ng*2 + 10]; 
    ng = group.enumerate(groups, false); 

    for (int i=0; i<ng; i++) { 
     listThreads(groups[i], indent + " "); 

} 
} 
} 

方法來發送消息:

class ChatThread extends Thread{ 
    static Vector<ChatThread> chatthread = new Vector<ChatThread>(2); 
    private String rslt; 
    private BufferedReader in; 
    private PrintWriter out; 
    private Socket sock; 


    public ChatThread (Socket socket) throws IOException { 
     this.sock = socket; 
     in = new BufferedReader(
       new InputStreamReader(socket.getInputStream())); 
     out = new PrintWriter(
       new OutputStreamWriter(socket.getOutputStream())); } 

    public void run(){ 


     String line; 
     synchronized(chatthread) { 
     chatthread.addElement(this); } 
     String portnum = Integer.toString(sock.getPort()); 

     try { 

     line = in.readLine()+portnum; 
     String[] mssgin = line.split("\\."); 

     for(int i = 0; i < chatthread.size(); i++) { 

       ChatThread handler = 
       (ChatThread)chatthread.elementAt(i); 
       handler.out.println(line + "\r"); 

       if(teksmasuk[0].contentEquals("login")){ 
        MysqlConn ceklogin = new MysqlConn(); 
        rslt = ceklogin.login(line); 
        System.out.println(rslt); 
        handler.out.flush(); 


       }else if(mssgin[0].contentEquals("reg")){ 
        Registrasi regis = new Registrasi(); 
        rslt = regis.register(line); 
        System.out.println(rslt); 
     handler.out.flush(); 
       } 
       else {   
       System.out.println("Waiting..."); 
       }    

     } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
     } 
     finally { 
      try { 
       in.close(); 
       out.close(); 
       sock.close(); 
       } catch(IOException ioe) { 
       } finally { 
       synchronized(chatthread) { 
       chatthread.removeElement(this); 
       } 
       } 
     } 

    } 

} 
+0

你可以把你的代碼,而不是別人的?謝謝 – Riking

+0

@Riking服務器端或兩者? – user1388581

+0

你嘗試發送消息的部分 – Riking

回答

0

這有一些一般的設計問題。 你不應該試圖發送消息給特定的主題;您應該嘗試向特定客戶端發送消息。嘗試完全隔離彼此的線程;他們唯一的互動應該是通過他們彼此建立的渠道。

+0

謝謝你的建議,但我可以用什麼方法將它們分開? – user1388581

相關問題