簡體   English   中英

使用多線程Java服務器獲取客戶端ID

[英]Get client id using multithreaded java server

我目前正在嘗試創建一個多線程的Java套接字服務器。 我可以使用“ID; MESSAGE;在我的客戶端發送”私人“消息。

消息到達正確的客戶端,但問題是服務器始終顯示所有消息來自同一客戶端(客戶端ID 0),但他們沒有。

這是我的服務器http://pastebin.com/Dzh5Ynvj

服務器輸出

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

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();
    }
}

添加this.id = id; ClientHandler構造函數中。 應該這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM