title: Understanding getInputStream and getOutputStream
body: <p>Here is a code</p>
  <pre><code>import java.io.*;
  import java.net.*;
  public class Whois
  {
      public static void main(String[] args)
          throws Exception
      {
          // TODO Auto-generated method stub
          int c;
          Socket s = new Socket(&quot;whois.internic.net&quot;,43);
          *InputStream in = s.getInputStream();
          *OutputStream out = s.getOutputStream();
          String str = (args.length == 0 ? &quot;osborne.com&quot; : args[0] ) + &quot;\n&quot;;
          byte buf[] = str.getBytes();
          *out.write(buf);
          System.out.print(&quot;hey baby&quot;);
          while ((c=in.read()) != -1)
          {
              System.out.print((char) c);
          }
          s.close();
      }
  }
  </code></pre>
  <p>I have marked the statements that I have problems understanding. I do not understand what OutputStream object <code>out</code> will hold when it is assigned <code>s.getOutputStream()</code> and what is the need of passing <code>buf</code> to <code>out</code> by <code>out.write(buf)</code>.</p>
  <p>I have learned Input and Output Streams using files but I do not understand <code>getinputstream</code> and <code>outputstreams</code>. I have googled it, read it here on SO as well as from many different books and from oracle documents as well. Please discuss it in detail.</p>
  <p>I know how to read from files and how to write to them, but here I do not understand what is the need of passing <code>buf</code> array which holds only a string. What I mean to ask is that when in has the input stream of the socket why can't we directly just read from it?
  What exactly is a socket <code>inputstream</code> and <code>outputstream</code>?</p>
  <p>I found something here on SO here is the link &quot;https://stackoverflow.com/questions/12715321/java-networking-explain-inputstream-and-outputstream-in-socket&quot;, here an answer by DNA says</p>
  <blockquote>
  <p>In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data).&quot;</p>
  </blockquote>
  <p>This is confusing me, when outputStream is used to send data via socket what was the need of out.write(buf)? Why do we need to send &quot;google.com&quot; to outputStream?</p>