'java difference between BufferedReader and InputStream when using socket
try (
InputStream reader = socket.getInputStream();
OutputStream writer = socket.getOutputStream()
) {
byte[] bytes = new byte[4096];
int byteLength = reader.read(bytes);
String message = new String(bytes, 0, byteLength, StandardCharsets.UTF_8);
writer.write("response message".getBytes());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
OutputStream writer = socket.getOutputStream()
) {
String message = reader.readLine();
writer.write("response message".getBytes());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
I made server socket. It returns response message when it receives message from client socket. When I use only InputStream (first code), the client socket gets response message. However when I use BufferedReader (second code), the client socket cannot get response message.
The way I send response message is same. How I receive message is the only different.
What's wrong with the second code? What's the difference between them?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
