简体   繁体   中英

how to send a message to the list of active threads?

I made a simple chat application that connects the server and client using the thread. I want to send a message to all active clients. how to send a message to the list of active threads? I use the method flush () but failed to send message to all active clients I found a method to display the thread list in google as follows:

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 + "  ");

}
}
}

method to send message:

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);
                }
                }
        }

    }

}

There's some general design issues with this. You should NOT be trying to send a message to a specific Thread; you should be trying to send a message to a specific client. Try to completely segregate the threads from each other; their only interaction should be through the streams that they have set up to each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM