gpt4 book ai didi

java - 线程、套接字和流

转载 作者:行者123 更新时间:2023-12-01 05:57:51 27 4
gpt4 key购买 nike

在我的程序中,我想通过套接字将一些信息发送到另一台计算机。第一个套接字向服务器发送一些文本,当第二个套接字收到此信息时,它会向第一个套接字发送应答。问题是另一个线程收到答案,而我得到的唯一结果是死锁。这是服务器端:

else if(msgArray[0].compareTo("game_request") == 0){
if(userMap.containsKey(msgArray[1])){
Socket socketPlayerTwo = userMap.get(msgArray[1]);
otherPlayer = msgArray[1];
sendResult(socketPlayerTwo, "game_offer?"+username);
Boolean willPlay =Boolean.valueOf(recieveRequest(socketPlayerTwo).split("?")[1]);
if(willPlay)
playingUser.put(username,msgArray[1]);
sendResult(socket, "game_accept?"+willPlay);

}
}

这是客户端:

private void showGameRequest(String usernameOther) {
try{
int status = GameWindow.getInstance().showNotification("Game Offer","The user " + usernameOther + " offers you a game!\nDo you agree?",SWT.ICON_QUESTION|SWT.YES|SWT.NO);
if (status == SWT.YES){
otherPlayer = usernameOther;
sendRequest("send_message_to_user?user="+usernameOther+"&value=true");
GameWindow.getInstance().getDisplay().asyncExec(new Runnable() {

@Override
public void run() {
GameWindow.getInstance().startNewGame();

}
});
}
else
sendRequest("send_message_to_user?user="+usernameOther+"&value=false");
}
catch (IOException exc){

}
}
<小时/>

这是 sendRequest 方法:

private void sendResult(Socket socket,String request) throws IOException{
DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
writer.writeUTF(request);
writer.flush();
}

客户端套接字在 Server 类中创建

while (true) {
try {
Socket socket = serverSocket.accept();
new GameThread(socket,databaseManager);
} catch (IOException e) {
e.printStackTrace();
}
}

当用户通过登录级别时将其放入hashmap中:

if(msgArray[0].compareTo("user_info") == 0){
Integer flagUser = -1;
String[] userInfo = {msgArray[1].substring(msgArray[1].indexOf("=") + 1, msgArray[1].indexOf("&")),msgArray[1].substring(msgArray[1].lastIndexOf("=")+ 1, msgArray[1].indexOf(";"))};
Boolean status = databaseManager.checkUser(userInfo[0], userInfo[1]);
if(status){
if(!userMap.containsKey(userInfo[0])){
userMap.put(userInfo[0], socket);
username = userInfo[0];
flagUser = 0;
}
else
flagUser = 1;
}
sendResult(socket, "user_info_status?"+flagUser.toString()+"");
}

我想我知道僵局的原因是什么,但我无法解决它。当第一个用户将信息发送给另一个用户时,他等待响应。当然,其他用户发送响应,但该响应是来自其他线程的句柄。所以死锁来自阻塞线程的读取方法。如何从一个套接字向另一个套接字发送信息而不发生死锁?

public GameThread(Socket socket, DatabaseManager databaseManager) {
this.socket = socket;
this.databaseManager = databaseManager;
parser = new RequestParser();
authorizationControl = new AuthorizationControl(databaseManager);
communication = new SocketCommunication(socket);
start();
}

最佳答案

您能给我们展示更多您的代码吗?

sendResult(socketPlayerTwo, "game_offer?"+username);
recieveRequest(socketPlayerTwo);
sendRequest("send_message_to_user?user="+usernameOther+"&value=true");

对于初学者来说。从一点来看,我发现您使用它们的顺序有问题。据我所知,您有一个 inputSteram.readObject() 方法,该方法已在某处阻塞等待来自另一端的消息。

关于java - 线程、套接字和流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2351397/

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