简体   繁体   中英

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.

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.

  1. You can try something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM