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 8748365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:30:47+00:00 2026-06-13T12:30:47+00:00

So now, I am making a client server app based multithread. In server side,

  • 0

So now, I am making a client server app based multithread. In server side, I make a thread for everysingle connection that accepted.

In thread class, I make a method that send a command to client. What i just want is, how to send a parameter to all running client? For simple statement, i just want to make this server send a message to all connected client.

I’ve been read this post and find sendToAll(String message) method from this link. But when i am try in my code, there is no method like that in ServerSocket .

Okay this is my sample code for server and the thread.

class ServerOne{

ServerSocket server = null;
...

ServerOne(int port){
            System.out.println("Starting server on port "+port);
    try{
      server = new ServerSocket(port);
              System.out.println("Server started successfully and now waiting for client");

    } catch (IOException e) {
      System.out.println("Could not listen on port "+port);
      System.exit(-1);
    }
}

public void listenSocket(){ 
    while(true){
        ClientWorker w;
        try{
            w = new ClientWorker(server.accept());
            Thread t = new Thread(w);
            t.start();
        } catch (IOException e) {
            System.out.println("Accept failed: 4444");
            System.exit(-1);
        }   
    }
}

protected void finalize(){
    try{
        server.close();
    } catch (IOException e) {
        System.out.println("Could not close socket");
        System.exit(-1);
    }
}
}

class ClientWorker implements Runnable{
Socket client;

ClientWorker(Socket client){
    this.client = client;
}
public void run(){
    ...
      sendCommand(parameter);
    ...
}

public void sendCommand(String command){
    PrintWriter out = null;
    try {
        out = new PrintWriter(client.getOutputStream(), true);
        out.println(command);
    } catch (IOException ex) {}
}

}

Thanks for help 🙂

  • 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-13T12:30:48+00:00Added an answer on June 13, 2026 at 12:30 pm

    The below answer, is not recommended for a full fledged server, as for this you should use Java EE with servlets, web services etc.

    This is only intended where a few computers want to connect to perform a specific task, and using simple Java sockets is not a general problem. Think of distributed computing or multi-player gaming.

    EDIT: I’ve – since first post – greatly updated this architecture, now tested and thread-safe. Anybody who needs it may download it here.

    Simply use (directly, or by subclassing) Server and Client, start() them, and everything is ready. Read the inline comments for more powerful options.


    While communication between clients are fairly complicated, I’ll try to simplify it, the most possible.

    Here are the points, in the server:

    • Keeping a list of connected clients.
    • Defining a thread, for server input.
    • Defining a queue of the received messages.
    • A thread polling from the queue, and work with it.
    • Some utility methods for sending messages.

    And for the client:

    • Defining a thread, for client input.
    • Defining a queue of the received messages.
    • A thread polling from the queue, and work with it.

    Here’s the Server class:

    public class Server {
        private ArrayList<ConnectionToClient> clientList;
        private LinkedBlockingQueue<Object> messages;
        private ServerSocket serverSocket;
    
        public Server(int port) {
            clientList = new ArrayList<ConnectionToClient>();
            messages = new LinkedBlockingQueue<Object>();
            serverSocket = new ServerSocket(port);
    
            Thread accept = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Socket s = serverSocket.accept();
                            clientList.add(new ConnectionToClient(s));
                        }
                        catch(IOException e){ e.printStackTrace(); }
                    }
                }
            };
    
            accept.setDaemon(true);
            accept.start();
    
            Thread messageHandling = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Object message = messages.take();
                            // Do some handling here...
                            System.out.println("Message Received: " + message);
                        }
                        catch(InterruptedException e){ }
                    }
                }
            };
    
            messageHandling.setDaemon(true);
            messageHandling.start();
        }
        
        private class ConnectionToClient {
            ObjectInputStream in;
            ObjectOutputStream out;
            Socket socket;
    
            ConnectionToClient(Socket socket) throws IOException {
                this.socket = socket;
                in = new ObjectInputStream(socket.getInputStream());
                out = new ObjectOutputStream(socket.getOutputStream());
    
                Thread read = new Thread(){
                    public void run(){
                        while(true){
                            try{
                                Object obj = in.readObject();
                                messages.put(obj);
                            }
                            catch(IOException e){ e.printStackTrace(); }
                        }
                    }
                };
    
                read.setDaemon(true); // terminate when main ends
                read.start();
            }
    
            public void write(Object obj) {
                try{
                    out.writeObject(obj);
                }
                catch(IOException e){ e.printStackTrace(); }
            }
        }
    
        public void sendToOne(int index, Object message)throws IndexOutOfBoundsException {
            clientList.get(index).write(message);
        }
    
        public void sendToAll(Object message){
            for(ConnectionToClient client : clientList)
                client.write(message);
        }
    
    }
    

    And here for the Client class:

    public class Client {
        private ConnectionToServer server;
        private LinkedBlockingQueue<Object> messages;
        private Socket socket;
    
        public Client(String IPAddress, int port) throws IOException{
            socket = new Socket(IPAddress, port);
            messages = new LinkedBlokingQueue<Object>();
            server = new ConnecionToServer(socket);
    
            Thread messageHandling = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Object message = messages.take();
                            // Do some handling here...
                            System.out.println("Message Received: " + message);
                        }
                        catch(InterruptedException e){ }
                    }
                }
            };
    
            messageHandling.setDaemon(true);
            messageHandling.start();
        }
    
        private class ConnectionToServer {
            ObjectInputStream in;
            ObjectOutputStream out;
            Socket socket;
    
            ConnectionToServer(Socket socket) throws IOException {
                this.socket = socket;
                in = new ObjectInputStream(socket.getInputStream());
                out = new ObjectOutputStream(socket.getOutputStream());
    
                Thread read = new Thread(){
                    public void run(){
                        while(true){
                            try{
                                Object obj = in.readObject();
                                messages.put(obj);
                            }
                            catch(IOException e){ e.printStackTrace(); }
                        }
                    }
                };
    
                read.setDaemon(true);
                read.start();
            }
    
            private void write(Object obj) {
                try{
                    out.writeObject(obj);
                }
                catch(IOException e){ e.printStackTrace(); }
            }
    
    
        }
    
        public void send(Object obj) {
            server.write(obj);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a mostly client side JavaScript / JQuery app that processes data
I'm making a game client/server and I'm having a new thread update some info
I am making a server-client project. Here the server makes changes in database based
I am making a multiplayer networking game. Now to connect to the server, client
I am making an app in which I get text from my server and
I'm migrating from server side development (java, php) to client side - HTML, CSS,
I'm making a client program in C that has to deal with this situation:
I have a client-server program that uses sockets to transfer information in a stateful
I'm making two programs; a server application (single instance), and a client application (multiple
I'm working on a little client that interfaces with a game server. The server

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.