gpt4 book ai didi

java - 根据某些参数获取对另一个线程的引用

转载 作者:行者123 更新时间:2023-12-02 07:44:41 25 4
gpt4 key购买 nike

我正在开发一个 Java 客户端/服务器聊天应用程序,该应用程序允许 LAN 内的用户在它们之间聊天。

在服务器应用程序中,我有一个主类,每当客户端连接到服务器时,它就会创建一个不同的线程。因此,基本上我与在单独线程上运行的每个客户端保持打开的 TCP 连接。

我在发送消息时遇到问题。当用户向服务器发送新消息+消息的目的地时,服务器必须找到与目的地用户有连接的线程并将该消息发送给他。我该怎么做呢。我已将每个线程与一个用户对象相关联,因此我需要执行类似 findThreadByUser(user) 的操作,并在获取引用后调用发送消息的方法。我该怎么做?

以下是实现 Runnable 的 ConnectionHandler 类的部分内容。我想访问该类的特定实例,更具体地说是其末尾的方法 sendToClient(String message) (外部 run 方法)。我尝试使用 HashMap 来存储用户和线程,但无法访问方法 sendToClient(String message)

public class ConnectionHandler implements Runnable {

protected Socket socket;
private ServerData serverData;
private User currentUser;
BufferedReader inFromClient;
PrintStream outToClient;
String clientSentence;
String peerIp;
String peerHostName;

public ConnectionHandler(Socket socketToHandle, ServerData serverData) {
socket = socketToHandle;
this.serverData = serverData;
inFromClient = null;
outToClient = null;
currentUser = null;

clientSentence = " ";
}

@Override
public void run() {
peerIp = socket.getInetAddress().getHostAddress();
peerHostName = socket.getInetAddress().getHostName();
try {
outToClient = new PrintStream(socket.getOutputStream(), true);

/* Create a reading stream to the socket */

inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(ConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error creating buffered handles.");
}

System.out.println("Handling connection to client at " + peerIp + " --");

Runnable r1 = new Runnable() {

public void run() {
while (true) {
try {
/* Read client's message through the socket's input buffer */
clientSentence = inFromClient.readLine();
} catch (IOException e) {
System.out.println(socket.getInetAddress() + "-" + peerIp + " broke the connection.");
break;
}
/* Output to screen the message received by the client */
System.out.println("Message Received: " + clientSentence);

List<String> processedClientSentence = ClientMessageProcessor.process(clientSentence);

if (processedClientSentence.get(0).equals(new String("LOGIN"))) {
String result = ServerActions.login(peerIp, peerHostName, processedClientSentence.get(1), serverData);

if (result.equals("success")) {
currentUser = serverData.getUserByIP(peerIp);
outToClient.println("USERS \"" + ServerActions.getUsers(serverData) + "\"");
} else {
outToClient.println("ERROR \'" + result + "\'");
}
} else if (processedClientSentence.get(0).equals(new String("GETUSERS"))) {
outToClient.println("USERS \"" + ServerActions.getUsers(serverData) + "\"");
} else if (processedClientSentence.get(0).equals(new String("USERINFO"))) {
} else if (processedClientSentence.get(0).equals(new String("MESSAGE"))) {
ServerActions.sendMessage(currentUser/*.getUserName()*/, processedClientSentence.get(1), processedClientSentence.get(2), serverData);
} else if (processedClientSentence.get(0).equals(new String("LOGOUT"))) {
} else {
}

}
}
};
new Thread(r1).start();
}

public void sendToClient(String message) {
outToClient.println(message);
}

}

最佳答案

将 Client 对象存储在 HashMap 中,将 UserName (唯一)作为 Key,将与其关联的线程作为 Value...`

关于java - 根据某些参数获取对另一个线程的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com