Bowen Li
Bowen Li

Reputation: 63

how does the getOutputStream() of Socket work?

  { Socket s = new Socket("xxx.xx.xx.xx",10004);

    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

    PrintWriter out = new PrintWriter(s.getOutputStream(),true);

    BufferedReader bufIn  = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String line = null;     
    while((line=bufr.readLine())!=null){
         if("over".equals(line))
             break;

         out.println(line);

         String upperStr = bufIn.readLine();
         System.out.println(upperStr);
    }

    s.close();}

so does the out.println(line); mean 1. the string which was entered will appear on the screen and 2.the content will be sent to the server socket at the same time? Thanks, guys.

Upvotes: 4

Views: 3562

Answers (1)

akaIDIOT
akaIDIOT

Reputation: 9231

Your variable out is a PrintWriter, but that doesn't mean that its something that will be printed on the screen. In this case, you gave it something that is the output stream of a socket, so it will print a line to the socket. If you want it to appear on the screen as well, you'll have to call something like System.out.println(line) as well.

Upvotes: 4

Related Questions