#$@ok
“#$ @”是不是我的字符串的字节,我不知道那里在做什么。
这是我的客户端功能,它连接到服务器发送数据并等待服务器发送:
public void connectToServer(String ip, int port, String boardSize) throws UnknownHostException, IOException, ClassNotFoundException {
server = new Socket(ip, port);
serverInput = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter serverOutput = new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
ObjectOutputStream player1OutputData = new ObjectOutputStream(player1.getOutputStream());
ObjectOutputStream player2OutputData = new ObjectOutputStream(player2.getOutputStream());
serverOutput.println(boardSize);
serverOutput.flush();
String isGameStarted = serverInput.readLine();
if(isGameStarted.equals("ok")) {
...
}
}
这是我的服务器代码,它发送“ok”字符串:
BufferedReader player1Input = new BufferedReader(new InputStreamReader(player1.getInputStream()));
BufferedReader player2Input = new BufferedReader(new InputStreamReader(player2.getInputStream()));
PrintWriter player1Output = new PrintWriter(new OutputStreamWriter(player1.getOutputStream()));
PrintWriter player2Output = new PrintWriter(new OutputStreamWriter(player2.getOutputStream()));
String gameStarted = "ok"
player1Output.println(gameStarted);
player1Output.flush();
player2Output.println(gameStarted);
player2Output.flush();
System.out.println("message sent to clients: \"" + gameStarted + "\"");
player1OutputData.writeObject(gameBoard);
player2OutputData.writeObject(gameBoard);
我没有发送任何垃圾数据,为什么会在那里? 我应该怎么做才能把它清理干净? 谢谢!
编辑:好的,所以我想我知道发生这种情况的时候, 在发送“ok”之后我想向客户端发送字符串矩阵,所以当我出于某种原因这样做时,“String isGameStarted = serverInput.readLine();” line没有获得“ok”字符串,它正在获取数组,并且在我删除那些行(我发送数组)之后,垃圾字节仍然存在,我不知道为什么。