I am doing a Server/Client network. The messages are send as Json Objects. I have a method for writing the Json-Object but my method for reading the Json-Object doesnt work. I dont have any exceptions but there is no output like excpected. Something must be wrong with the bufferedReader. I dont know how to get that Json-Object from the socket who sent it. Method for writing:
public void writeMessage(JSONObject json) {
try {
PrintWriter printWriter = new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()));
printWriter.print(json);
printWriter.flush();
} catch (IOException writeMessageException) {
System.out.println("!");
}
}
method for reading the message/ receiving the message:
private static void readMessageFromServer(Socket socket) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())))
{
StringBuilder sb = new StringBuilder();
String readLine;
while ((readLine = br.readLine()) != null) {
sb.append(readLine);
}
JSONObject js = new JSONObject(sb.toString());
String action1 = (String) js.get("action1");
System.out.println(action1);
} catch(IOException e) {
System.out.println("!");
}
}
Thank you :)