简体   繁体   中英

Narrow communication to a specific client in chat application java

I just created an java chat application that allow a communication between many clients, however, i would like also to have the ability to one client send a message to a specific client, the others client cannot be able to see the message send, just like one client whispering to another one. Thank you in advance.

Here some part of my client

    public void sendListener(){
        writer.println(clientName2+" : "+broadcastTF.getText() );
        writer.flush();
        broadcastTF.setText("");
        broadcastTF.requestFocus();
    }

    public class listenServer implements Runnable{

        public void run(){
            try{
         String text;
            while((text = reader.nextLine()) != null){
                messageTA.append(text+ "\n");
            }
            }catch(Exception ex){}
        }
    }


private void setupServer(){
    try{
    socket = new Socket("127.0.0.1", 7894);
    writer = new PrintWriter(socket.getOutputStream());
    reader = new Scanner(socket.getInputStream());
    listenServer ls = new listenServer();
    Thread t = new Thread(ls);
    t.start();
    }
    catch(Exception ex){

    }
}

Here some part of my server

public class listenToClient implements Runnable{
Scanner reader;
public listenToClient(Socket socket){
    try{
        reader = new Scanner(socket.getInputStream());
    }catch (Exception ex){}
}

public void run(){
    try{
        String text;
        while((text = reader.nextLine()) != null){
                            sendToAll(text);

        }
    }catch(Exception ex){}
}

public void sendToAll(String text){
    for (PrintWriter w : writers){
        try{
            w.println(text);
            w.flush();
        }catch(Exception ex){}
    }
}

}

I think this is less related to specific code, and more related to overall design.

Primarily, you'd need some way of identifying individual clients. If you need fast lookups, you would use some sort of key/value map to store your writers , with the unique ID (user name, user ID, some random string, or whatever suits your situation) as the key. If that's not a concern, then you can store your client connections in a simple numerical array (or array-like structure) and iterate over until you find the target, then send to exclusively that connection.

Then, the sending client needs to be able to discern what its target is, and also have some way of sending the target information along with the message. Simply reading input at the server and echoing it out will not suffice for this - you'll have to do some amount of parsing, and you'll probably need to design some format for arranging that information.

As an example, one client-server communication format I designed had the first byte of a message be the length of the key in a key/value pair. The server would read the first byte, then read the next N bytes as the key. The remaining bytes were then known to be the value. In your case, a key length of 0 would mean it's not going to one specific destination, but to everyone.

Create a collection of clients such as hashmap. So whenever you get a new client connection accept, assign this client an id and put its socket reference against the id in the hashmap.

Clients should know each other ids to make sure that they can identify and send the messages to each other. So whenever a client sends a packet to server and it contains a recepient client id, server should lookup the hashmap. The recepient socket reference should be retrieved and message should be sent using the writer.

Hope it helps!

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