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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:19:34+00:00 2026-05-26T19:19:34+00:00

I am creating a program where multiple clients can connect to a server. The

  • 0

I am creating a program where multiple clients can connect to a server. The message sent by a client will be broadcast to all other client connections on the server.

My problem is that the message is broadcast to only the client it has come from, and I cannot spot the error in my code.

Can anyone help me spot where the problem is or how I could improve the code?Thank you.

EDIT:

public class MsgClient{

private Socket client;
private ObjectInputStream input;
private DataOutputStream output;
private BufferedReader keyboard;
private String cmdInput;


public MsgClient(String name, String server, int port){

    try{

        client = new Socket(server, port);

        DataInputStream sInput = new DataInputStream(client.getInputStream());
        output = new DataOutputStream(client.getOutputStream());
        input = new ObjectInputStream(client.getInputStream());
        keyboard = new BufferedReader(new InputStreamReader(System.in));


        output.writeUTF(name);


        while(true){
            System.out.println("Send a msg to the server: ");
            cmdInput = keyboard.readLine();
            output.writeUTF(cmdInput);
            System.out.println(sInput.readUTF());
        }

    }
    catch (Exception e){
        e.printStackTrace();
    }   
}// end constructor


public static void main(String args[]) throws IOException {
    if(args.length != 3)
        throw new RuntimeException("Syntax: java MsgClient <username> <servername> <port>");
    MsgClient aClient = new MsgClient(args[0], args[1], Integer.parseInt(args[2]));
} // end main

}

public class MsgServer {


public MsgServer(int PORT) throws IOException{

    ServerSocket server = new ServerSocket(PORT);
    System.out.println("Server Established...");


    while(true){

        Socket client = server.accept();

        DataInputStream input = new DataInputStream(client.getInputStream());
        ObjectOutputStream oo = new ObjectOutputStream(client.getOutputStream());
        DataOutput output = new DataOutputStream(client.getOutputStream());

        System.out.println("New client accepted");

        String clientName = input.readUTF();
        ClientHandler handler = new ClientHandler(clientName, client);  // construct and run thread.

        handler.start();
        System.out.println("Handler started!");

    }//end while

}//end of constructor


public static void main(String args[]) throws IOException {
    if(args.length != 1)
        throw new RuntimeException("Syntax: java MsgServer requires <PORT> number");
    new MsgServer(Integer.parseInt(args[0]));
}

}

public class ClientHandler extends Thread {

Socket client;
DataInputStream din;
DataOutputStream dout;
String name;

String clientMsg;

protected static Vector socketVector = new Vector();


public ClientHandler (String name, Socket client) throws IOException{
    this.name = name;
    this.client = client;
    din = new DataInputStream(client.getInputStream());
    dout = new DataOutputStream(client.getOutputStream());
}

// Code run at every start()
public void run(){
    try{
        socketVector.addElement(this);      
        clientMsg = din.readUTF(); // inside or outside loop?

        while(true){
            broadcast( name + " has joined auction on IP " + client.getInetAddress());
            broadcast( name + " says: " + clientMsg);
        }

    } catch(IOException ex){
        System.out.println("-- Connection to user lost");
    } finally{
        socketVector.removeElement(this);
        broadcast(name + " has left");
        try{
            client.close();
        }catch (IOException ex){
            System.out.println("socket to user already closed?");
        }
    }
}
  • 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-05-26T19:19:34+00:00Added an answer on May 26, 2026 at 7:19 pm

    Another issue is here, in the MsgClient code:

    cmdInput = keyboard.readLine();
    output.writeUTF(cmdInput);
    System.out.println(sInput.readUTF());
    

    A client will not receive a message until after it has sent one.

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

Sidebar

Related Questions

I am creating a program that will multiple background tasks simultaneously. I would like
Most often when creating multiple classes inside a program that use each other, I
I am creating a program that will be installed using the .net installer project.
I have been tasked with creating a program that will generate an amortization schedule.
I have been tasked with creating a program what will create take files in
I have a program that is creating multiple files. There is a function for
I am creating a custom .net hardware framework that will be used by other
I'm trying to write a program that uses sockets to connect with other instances
My program needs to send data to multiple (about 50) client stations. Important bits
I am creating a program that will run an experiment on a user. It

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.