Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:35:37+00:00 2026-06-03T22:35:37+00:00

I made a simple chat application that connects the server and client using the

  • 0

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

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:35:39+00:00Added an answer on June 3, 2026 at 10:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We made a simple application and using GoogleAppEngineLauncher (GAEL) ran that locally. Then we
I've made simple nodejs application by using nodejs+express. Now I want to make user
I made a simple application that adds 1 to the count and displays the
I have made a simple chat script using jquery and AJAX but the huge
I made simple email in wordpress but it would only send email to the
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made this simple function to filter the data. I add the symbols that
I've made a chat application for a website. Everything is working fine, however, I
i made simple web server like below. import BaseHTTPServer, os, cgi import cgitb; cgitb.enable()
I made a small page ( http://www.ovlu.li ) using CMS Made Simple ( http://www.cmsmadesimple.org/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.