gpt4 book ai didi

java - 套接字多线程死锁

转载 作者:行者123 更新时间:2023-12-01 11:26:22 25 4
gpt4 key购买 nike

我有一个与 this 类似的问题,但我知道当我要求读取一行时,发件人应该发送行尾。

令我困惑的是,在调试中,它是有效的。可能是因为我在调试时跳过的顺序(直到现在我什至不知道这会产生影响),但我想更好地理解它。

我已经使用过线程,但不是很多。

这是我的服务器类:

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {

protected static List<Game> games = new ArrayList<>();
protected static List<ServerThread> players = new ArrayList<>();

public static void main(String[] args) throws Exception {
int serverPort = 8945;
Server server = new Server();
ServerSocket welcomeSocket = new ServerSocket(serverPort);

while (true) {
Socket connectionSocket = welcomeSocket.accept();
ServerThread st = new ServerThread(server,connectionSocket);
st.start();
int gameId = 0;
if(players.size()>0 && players.size()%2==0){
gameId++;
players.get(0).outToClient.write("START " + gameId
+ " 123 456" +"\n");
players.get(0).outToClient.flush();
players.get(1).outToClient.write("START " + gameId
+ " 456 123" +"\n");
players.get(1).outToClient.flush();
}
}
}
}

线程(基于this)

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread {
protected Server server;
protected Socket socket;
protected String playerName;
protected BufferedReader inFromClient;
protected BufferedWriter outToClient;

public ServerThread(Server server, Socket clientSocket) throws IOException {
this.server = server;
this.socket = clientSocket;
this.inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.outToClient = new BufferedWriter(new InputStreamReader(socket.getOutputStream()));
}

public void run() {
while (true) {
try {
String line = inFromClient.readLine();
if(line != null) {
String[] clientCommand = line.split(" ");
String commandType = clientCommand[0];
if (!commandType.equalsIgnoreCase("QUIT")) {
switch (commandType) {
case "JOIN":
playerName = clientCommand[1];
System.out.println(playerName + " joined");
Server.players.add(this);
break;
case "PLAY":
//nothing yet
break;
case "MSG":
//nothing yet
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}

客户:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Client {

private static int gameID;
private static int order;
private static String opponent;

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java Client <serverIp>");
System.exit(1);
}
String serverIP = args[0];
int serverPort = 8945;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(serverIP, serverPort);
BufferedWriter outToServer = new OutputStreamWriter(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String line = inFromUser.readLine();
String[] commandSentence = line.split(" ");
String userCommandType = commandSentence[0];
while (!userCommandType.equals("/exit")){
switch (userCommandType){
case "/enter":
String nickname = commandSentence[1];
outToServer.write("JOIN "+ nickname + '\n');
outToServer.flush();
while (true){
String serverLine = inFromServer.readLine();
String[] serverCommand = serverLine.split(" ");
String serverCommandType = serverCommand[0];
if(serverCommandType.equalsIgnoreCase("START")){
gameID = Integer.parseInt(serverCommand[1]);
order = Integer.parseInt(serverCommand[2]);
opponent = serverCommand[3];
System.out.printf("%5s %5s %5s",gameID,order,opponent);
break;
}
}
case "/play":
//nothing yet
break;
case "/msg":
//nothing yet
break;
}
}
}
}

看起来它在某个地方进入了死锁,并且出于某种原因,永远不要在向客户端发送数据的Server类上输入该死锁,除非在调试中运行

(顺便说一句,我使用 get(0)get(1) 仅用于测试目的)

编辑:好吧,我的愚蠢错误是当客户端向服务器发送数据时我忘记添加outToServer.flush();。但我的主要问题仍然存在,当我通过在每个客户端中键入“/enter ”来创建两个客户端时,当最后一个客户端添加到列表中时,预计会输入该 if 语句服务器。

最佳答案

问题出在将命令发送到服务器的行的客户端代码处。您发送的字符串长度非常小,因此需要 outToServer.flush();才能正常工作

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

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