在下面的代码中,我正在实现一个基本的服务器客户端骰子游戏。但是,客户端扫描仪似乎没有读取任何内容
服务器类别:
public class ServerClass {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(3300);
System.out.println("Server started on port " + server.getLocalPort() + " and " + server.getInetAddress());
while (true) {
Socket client = server.accept();
ClientHandler clientHandle = new ClientHandler(client);
Thread thread = new Thread(clientHandle);
thread.start();
}
}
}
客户端类:
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket server = new Socket("localhost", 3300);
Scanner scanner = new Scanner(System.in);
BufferedReader input;
PrintWriter output;
int clientDice;
int serverDice;
try {
input = new BufferedReader(new InputStreamReader(server.getInputStream()));
output = new PrintWriter(server.getOutputStream(), true);
System.out.println(input.readLine());
String playing = "play";
while ("play".equals(playing)) {
System.out.println(input.readLine());
playing = scanner.nextLine();
output.println(playing);
if ("play".equals(playing)) {
serverDice = Integer.parseInt(input.readLine());
clientDice = new Random().nextInt(6);
System.out.println("Client rolled " + clientDice + ". Server rolled " + serverDice);
} else {
playing = "exit";
}
}
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
ClientHandler 类:
public class ClientHandler implements Runnable {
public Socket client;
public ClientHandler(Socket client) {
this.client = client;
}
public void run() {
Scanner scanner = new Scanner(System.in);
BufferedReader input;
PrintWriter output;
int serverDice;
try {
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream(), true);
System.out.println("New client connected");
System.out.println("Server is ready to play");
String playing = "play";
while ("play".equals(playing)) {
output.println("Would you like to play?");
playing = input.readLine();
if ("play".equals(playing)) {
output.println(new Random().nextInt(6));
}
}
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
我输入“play”什么也没发生,它甚至不识别输入
您应该检查服务器和客户端事件的时间顺序以及它们何时读取或写入数据。当客户端连接到服务器时,服务器会发送字符串“你想玩吗?”通过
output.println("Would you like to play?"); /* server */
给客户。之后它将等待
playing = input.readLine(); /* server */
客户的任何回应。在客户端,它将读取并打印来自服务器的消息
System.out.println(input.readLine()); /* client */
客户将阅读并打印最初的“你想玩吗?”来自服务器的消息。但之后,在
while()
循环内,它将使用 从服务器读取任何 附加 数据
System.out.println(input.readLine()); /* client */
再次。由于服务器正在等待客户端的输入并且不发送任何数据,而客户端正在等待服务器的输入并且不发送任何数据,因此双方都在等待另一方的输入,但他们都没有发送任何数据。这会导致僵局。
您可能想要添加额外的信息/调试行来检查客户端和服务器在哪个时间点正在做什么。作为一个例子,你可以写
System.out.println("[debug] Going to read data from the server");
System.out.println(input.readLine());
System.out.println("[debug] Read of data complete");
和
System.out.println("[debug] Going to send the data '"+playing+"' to the server");
output.println(playing);
System.out.println("[debug] Send of data complete");
问题的解决方案可能就像删除线路一样简单
System.out.println(input.readLine());
从
while
循环上方开始,因为您已经在 while
循环内读取数据