3

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

Krimson
  • 7,386
  • 11
  • 60
  • 97
  • 1
    You never _write_ to a standard _input_ stream. – Mat Sep 01 '14 at 20:37
  • @Mat Yeah you are right! Typo on my end. – Krimson Sep 01 '14 at 20:40
  • It would be useful to add a link to the actual javadoc, but I think you're right that it's not correct. – wvdz Sep 01 '14 at 20:43
  • @popovitsj ahh, I'm screwing up! There, done it! – Krimson Sep 01 '14 at 20:44
  • You need to understand the Unix process paradigm. Every process has a stdin and stdout pipe, and you can link processes end-to-end with stdout from one step going to stdin of the next. Even though that may not be how it actually works in, eg, a Windows environment, the terminology is still used. – Hot Licks Sep 01 '14 at 20:48

2 Answers2

2

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • `the client reads this from STDIN and sends to server via socket.` I get that part but what about when the client has to get a response/data from the server? It would read from the server's `stdIn` right? – Krimson Sep 01 '14 at 20:48
  • Please see my edit to original post. I hope this helps. – AlexR Sep 01 '14 at 20:54
  • This is exactly what I expected. So then why does the tutorial say `To get the server's response, EchoClient reads from the BufferedReader object stdIn`. Shouldn't it be `To get the server's response, EchoClient reads from the BufferedReader object in` ? – Krimson Sep 01 '14 at 20:56
  • 1
    I read your quotes again and think that you probably right. However I do not see full text, so I cannot be sure. Anyway it seems that you understand the code correctly. This is more important than understanding each sentence in tutorial. – AlexR Sep 01 '14 at 21:05
1
BufferedReader stdIn =
        new BufferedReader(
            new InputStreamReader(System.in))

stdIn is defined there, it is not the same as System.in.

folkol
  • 4,752
  • 22
  • 25
  • yes but how does the client read data from the server? from `BufferedReader stdIn` or `BufferedReader in` ? – Krimson Sep 01 '14 at 20:53
  • Let me rephrase your question. "How does the client read data from the server? From the socket connected to the server (in), or from the client's local standard input stream? (stdIn)" – Gimby Sep 01 '14 at 21:32
  • 1
    Oh, I see your points now. And yes, that seems to be a typo. (I was checking the code on the tutorial page, and that does indeed read from "in"). – folkol Sep 01 '14 at 21:38