Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a typo in the java tutorials?

I was going through the Java TCP Client Server tutorials where they are explaining how a echo server works and how a TCP client interacts with the echo server.

For the TCP Client, they have given this snippet and are 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 on, a few lines below (around 8 lines) they say:

To send data through the socket to the server, the EchoClient example needs to write to the PrintWriter. To get the server's response, EchoClient reads from the BufferedReader object stdIn, which is created in the fourth statement in the try-with resources statement.

Why does it say that

To get the server's response, EchoClient reads from the BufferedReader object stdIn

Isn't stdIn used for reading from the System's standard input and not the socket's standard input? Should the Echo client read from the in BufferedReader?

If I am wrong, could you clarify my misunderstanding?

http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html


Edit

Maybe I wasnt clear enough. When the client has to get data from the server. Does it get it form this:

BufferedReader in =
            new BufferedReader(
                new InputStreamReader(echoSocket.getInputStream()));

or this:

BufferedReader stdIn =
            new BufferedReader(
                new InputStreamReader(System.in))

The doc says from the second one. That doesn't make sense, it should be the first one

like image 895
Krimson Avatar asked Mar 25 '26 20:03

Krimson


1 Answers

The EchoClient is a kind of simple shell application (like telnet). It allows user to type something to console, the client reads this from STDIN and sends to server via socket. So, tutorial explains everything correctly, I think.

EDIT

Client is reading response from server using input stream in and reading the user's command using stream stdIn. Take a look on the source code you posted.

like image 73
AlexR Avatar answered Mar 28 '26 10:03

AlexR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!