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

  • Home
  • SEARCH
  • 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 8724011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:44:32+00:00 2026-06-13T07:44:32+00:00

Hello everyone i have created a multi threaded chat server that looks like this:

  • 0

Hello everyone i have created a multi threaded chat server that looks like this:

    public class Main {

    public static ServerSocket server;
    public static Socket connection;
    public static int backLog = 100;
    public static int numberOfConnected;
    public static boolean connected = false;
    public final static int potNumber = 6080;
    public static PrintWriter pw;
    public static Scanner input;
    public static int i = 0;

    public static void main(String[] args) {
        startServer();

    }
    public static void startServer(){
        try {
            server = new ServerSocket(potNumber, backLog);
            waitingForConnection();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void waitingForConnection() {
        connected = false;
        i++;
        while (!connected) {
            try {
                if (connected) {

                }
                connection = server.accept();
                Server s = new Server(connection, pw = new PrintWriter(connection.getOutputStream()), input = new Scanner(connection.getInputStream()));
                s.start();
                numberOfConnected++;
                waitingForConnection();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

The idea is that this is suppose to be a chat server so when one connects to the server it starts the following thread:

threads

    public void run(){
    while (connection.isConnected()) {
        if (input.hasNext()) {
            String fullMessage = input.nextLine();
            if (fullMessage.equalsIgnoreCase("Connect")) {
                connectHim();
            }else {
                chatMessage(fullMessage);
            }
        }

        }
    }

private void chatMessage(String fullMessage) {
    String name = fullMessage.substring(0, fullMessage.indexOf(" "));
    String message = fullMessage.substring(fullMessage.indexOf(" "), fullMessage.length());
    pw.println(name+": "+message);
    pw.flush();

}
private void connectHim() {
    String name = input.nextLine();
    pw.println(0);
    pw.flush();
    pw.println(1);
    pw.flush();
    pw.println();
    pw.flush();
    pw.println(name);
    pw.flush();

}

So my problem is the following:

if the user that is bound to thread 1 (this is an example) and the user bound to thread 2 sends a message to the server how will i send that message to the user bound on thread 1?

  • 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-13T07:44:33+00:00Added an answer on June 13, 2026 at 7:44 am

    One of options is to use Hashtable or HashMap (just call Collections.synchronizedMap(myMap) in case of Map usage). When you start new Thread, give him unique name (for example user nick name ) and put it to your collection where key – Thread name, and value – Thread as Object.

    if the user that is bound to thread 1 (this is an example) and the user bound to thread 2 sends a message to the server how will i send that message to the user bound on thread 1?

    For example you have user1, user2, user3. Now you build 3 Threads and put them to HashMap, like:

    Map<String, Thread> threadMap = new HashMap<String,Thread>();
        threadMap = Collections.synchronizedMap(threadMap);
    
    
    
        YourThread th1 = new YourThread();
                threadMap.put("user1", th);
    
        YourThread th2 = new YourThread();
                threadMap.put("user2", th);
    
        YourThread th3 = new YourThread();
                threadMap.put("user3", th);
    
                 ....
    
        Set<String> userSet = threadMap.keySet();
    
        Iterator<String> it = userSet.iterator();
    
        Thread currThread = null;
    
        while(it.hasNext()){
            String key = it.next();
    
            currThread = threadMap.get(key);
    
            // do something with currThread         
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone~ I have a problem to solve~ I have created a server that
Hello everyone (this is my first post) I have some simple AD code that
Hello again everyone, This time I have a problem with Intent & Extras. here's
Hello everyone i currently have this: import feedparser d = feedparser.parse('http://store.steampowered.com/feeds/news.xml') for i in
hello everyone im trying to create this tweak that copies a file from one
Hello everyone I have a question that I will describe as good as I
hello everyone I have this snippet of the code: void* Init(int N) { Hand
hello everyone I have some problem with understanding this piece of the code: import
Hello everyone, I have been banging my head really hard trying to solve this
So hello everyone I have a big problem right now with a button that

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.