简体   繁体   English

使用多线程Java服务器获取客户端ID

[英]Get client id using multithreaded java server

I'm currently trying to create a multithreaded java socket server. 我目前正在尝试创建一个多线程的Java套接字服务器。 I can send "private" messages using "ID;MESSAGE; in my clients. 我可以使用“ID; MESSAGE;在我的客户端发送”私人“消息。

The messages arrive at the right client but the problem is that the server always displays that all messages come from the same client (client id 0), but they don't. 消息到达正确的客户端,但问题是服务器始终显示所有消息来自同一客户端(客户端ID 0),但他们没有。

Here's my Server http://pastebin.com/Dzh5Ynvj 这是我的服务器http://pastebin.com/Dzh5Ynvj

Server output 服务器输出

21:02:55 [DSS-Server] [Client#0] connected.
21:02:58 [DSS-Server] [Client#1] connected.
21:13:11 [DSS-Server] [Client#0] > 0;This is send from client 0
21:13:18 [DSS-Server] [Client#0] > 1;This also
21:13:30 [DSS-Server] [Client#0] > 0;But this comes from client 1

In your ClientHandler code, you have not assigned the id parameter to the id member of your class. ClientHandler代码中,您尚未将id参数分配给类的id成员。

public ClientHandler(Socket client, int id) {
   this.id = id; // ADD THIS LINE
    try {

        //Get BufferedReader from client
        this.client = client;
        reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Add this.id = id; 添加this.id = id; in your ClientHandler constructor. ClientHandler构造函数中。 That should do it. 应该这样做。

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

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