简体   繁体   English

服务器如何与客户端服务器系统中的特定客户端通信?

[英]How can the server communicate to a specific client in a Client-Server system?

By writing simple System.out.println() on server side, the message goes to all clients. 通过在服务器端编写简单的System.out.println() ,消息将发送到所有客户端。 But is there a way to make one client's address embedded to a java function which recognize that the server is sending a message to this specific client? 但是,是否有一种方法可以将一个客户机的地址嵌入到Java函数中,该函数可以识别服务器正在向该特定客户机发送消息?

This probably requires a bit more information to be specific, but I'll try and run with it. 这可能需要更多的信息来具体说明,但我将尝试并使用它。

So if you have a server that maintains a list of clients that are connected, you could ideally tag each one with some type of ID when they make a connection into the server. 因此,如果您有一台服务器维护着一组已连接的客户端,那么当它们与服务器建立连接时,最好在每个客户端上加上某种类型的ID。

For example lets say you had a server that managed some kind of data and two types of clients could connect: Managers and Customers. 例如,假设您有一台管理某种数据的服务器,并且可以连接两种类型的客户端:经理和客户。

Now when a client connects you can setup the server so that it asks the user which they are and verify with a login maybe. 现在,当客户端连接时,您可以设置服务器,以便它询问用户身份,并可能通过登录进行验证。 Continuing from that you could place all of the types into unique lists and then broadcast to those lists separately. 接下来,您可以将所有类型放入唯一列表,然后分别广播到这些列表。

Additionally since you can control the server and client application. 另外,因为您可以控制服务器和客户端应用程序。 You could generate a random ID number/tag/something for each user that connects and store that in a list. 您可以为每个连接的用户生成一个随机的ID号/标签/内容,并将其存储在列表中。 Then should the server want to message a unique client you just have to search the list(or hashmap) for that users socket/connection and broadcast to them. 然后,如果服务器要向唯一的客户端发送消息,您只需要在列表(或哈希图)中搜索该用户的套接字/连接并向其广播。

I know this answer seems a bit vague but the information provided felt that way. 我知道这个答案似乎有点含糊,但是所提供的信息就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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