hi there,
i have this server code that send the received message from one client to all the other clients, including the one how send the message

public class Chat extends Thread{
static LinkedList<PrintStream> sll;
Socket cs;
PrintStream ps;
    public Chat(Socket cs){
    this.cs=cs;
     try{
         ps=new PrintStream(cs.getOutputStream());
    sll.add(ps);
     this.start();
    }
    catch (Exception ex){ex.printStackTrace();}
    }
    
public void run(){
    try{ DataInputStream dis=new DataInputStream(cs.getInputStream());
while (true){
String s=dis.readLine();

for(PrintStream bps:sll) bps.println(s);
    


}
    }
    catch (Exception ex){
        
        ex.printStackTrace();
        sll.remove(ps);}

}
    public static void main(String[] args) throws Exception{
 sll=new LinkedList<PrintStream>();
    ServerSocket ss=new ServerSocket(9000);
    
    
    while(true)
    {
    Socket s=ss.accept();
    Chat cs=new Chat(s);
    
    }
    
    
    }
}

How to edit this code to make it send the received message to all clients except the one who send it??

thanks in advance

If you know the client that sent the message and have a reference to it, then use that reference as you send the messages to the clients in the list to skip over the client that sent the message.

I assume that you have a list of connected clients and that there are references to each in the list and that you can save a reference to the client that sent the message.

what do you mean by a reference to the client who sent the message?

should i make another list of Socket and add client to it?

can you give more explanation please

When a client connects how do you keep track of that connection? I assume that you create a class to manage the connection. When you create that class with new, you get a variable that refers to the new instance of the class.
Save that reference in a list. In your code, cs is a reference to an instance of the Chat class. Save that in a list.

i already made another link list of Socket to save cs but i don't know how to deal with it so i can skip sending the message to its sender.

If you know who sent the message and have a reference to the Chat client that sent it, you can use that reference to skip over the entry in the list who's reference matches that of messages' sender.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.