I am developing a client for a small game. Using sockets, I am parsing the request and responses with PrintWriter
and BufferedReader
classes.
The issue I have is when BufferedReader
is used for the second time (second response from server).
Example:
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.
What could be the issue here and what is the best way to solve this problem?