caff_91
caff_91

Reputation: 7

Java Client/Server Chat Application

This is the application that I have created but I have the following problems:

  1. The client is able to send messages to other client but they also receive the same message as echo. So I would like to just be able to send a message and get a response from all connected parties, but no my own message.

  2. What would it be the best way to handle disconnection from the client or the server.

  3. I'm not asking for an answer (just indication), on how would it be the best way to allow a client to send files.

Upvotes: 1

Views: 70

Answers (2)

duck codes
duck codes

Reputation: 41

  1. You can try something like this

    for (ClientHandler client : Server.activeClients) {
        if(client!=this){
            client.dos.writeUTF(this.name + "=" + received);
        }
    }
    

Upvotes: 0

Doğukan HAN
Doğukan HAN

Reputation: 121

1-) You can check the user in the for clause before send message.

for (ClientHandler client: Server.activeClients) {
   if (!client.name.equals(this.name)) {
      client.dos.writeUTF(this.name + "=" + received);
   }
}

2-) You don't need to use while(true) change it so it can finish when user disconnected.

while (!s.isClosed()) {

3-) First you need to send file name and file size after that you can send file's bytes in the sender. Receivers will take the bytes until reaches to file size and will save it as file with the given name.

Upvotes: 1

Related Questions