java chat sending users list from server to clients and update it

旧街凉风 提交于 2019-12-13 05:15:55

问题


I write client-server chat using sockets. Help me please understand how send the list of current registered users from server to clients and update it when list changed. I read this similar post but steel don't understand how to do it. I used BufferedReader and PrintWriter streams for sending and receiving messages. And I don't want sending the list of the users any time when clients send messages.

I keep list of users in a map as a key set on the server side.

public class Server{
    private final Map<String, ServerThread> clients = Collections.synchronizedMap(new TreeMap<>());
    //.....
}

I read on one forum, if use serialization, than all serializable classes have to be both the sending and the receiving side. But I don't need server class on client side.

UPDATE: If I will use ObjectOutputStream / ObjectInputStream instead BufferedReader and PrintWriter whether the following approach is useful for distinguishing messages from the list of user names?

//code on client side:
Object received = input.readObject();
if (received instanceof List) {
    List<String> users = (List) received;
    //display users in JList
} else if (received instanceof String) {
    String message = (String) received;
    //display in JTextArea
}

来源:https://stackoverflow.com/questions/25699232/java-chat-sending-users-list-from-server-to-clients-and-update-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!