작은 게임용 클라이언트를 개발 중입니다. 소켓을 사용하여 요청 및 응답을 구문 분석 할 때 PrintWriter
및 BufferedReader
클래스를 사용합니다.BufferedReader가 두 번째 출력을 읽지 않습니다.
문제점은 BufferedReader
이 두 번째로 사용 된 경우입니다 (서버의 두 번째 응답).
예 : 여기에 문제가이 문제를 해결하는 가장 좋은 방법은 무엇입니까 수 무엇
public class BotClient {
private final Socket socket;
private final PrintWriter writer;
private final BufferedReader reader;
public BotClient(final String gameId, final String botName) throws IOException {
this.writer = new PrintWriter(socket.getOutputStream(), true);
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
//Code that connects client to server. Works successfully.
public void sendGameRequest() {
try {
//This first is the FIRST request to server. This works SUCCESSFULLY.
writer.println("GET " + "/playerInfo/?gameId=" + gameId + "&playerName=" + botName + " HTTP/1.0\r\n");
reader.lines().forEach(System.out::println); // This will output game response successfully.
//Now the problem occurs below here. I could do the same request but won't get an output.
writer.println("GET " + "/playerInfo/?gameId=" + gameId + "&playerName=" + botName + " HTTP/1.0\r\n");
reader.lines().forEach(System.out::println); //EMPTY PRINT
// The above is EXACTLY the same request however this prints out nothing.
?
왜 HTTP 클라이언트를 사용하지 않습니까? – sturcotte06
서버가 아무 것도 또는 빈 문자열을 쓸 것입니다. 그것을 확인하십시오. – davidxxx
두 번째'lines()'을 호출하기 전에'close()'를 사용하여 첫 번째 스트림을 닫는 것은 어떨까요? –