Is this a typo in the java tutorials?
I've been going through the Java TCP Client Server tutorials where they explain how an echo server works and how a TCP client interacts with an echo server.
For a TCP client, they provided this snippet and explain what it is:
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
)
later, a few lines below (about 8 lines), they say:
To send data over a socket to the server, the EchoClient example needs to be written to the PrintWriter. To get the server's response, the EchoClient reads stdIn from the BuffedReader object, which is created in the fourth expression in the try-with resources statement.
Why is it said that
To get the server's response, EchoClient reads stdIn from BufferedReader object
Not used stdIn
to read from system stdin rather than socket stdin? Should the Echo client read the in
BufferedReader?
If I'm wrong, could you please clear up my misunderstanding?
http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
Edit
Perhaps I was not clear enough. When the client should receive data from the server. Does this get the form:
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
or that:
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
Doc speaks from the second. It doesn't make sense, it should be the first
source to share
EchoClient
is a simple shell application (like telnet). It allows the user to enter something in the console, the client reads this from STDIN and sends it to the server over a socket. So the tutorial explains everything correctly I guess.
EDIT
The client reads the response from the server using the input stream in
and reads the custom command using the stream stdIn
. Take a look at the source code you posted.
source to share