Understanding getInputStream and getOutputStream

2019-01-22 05:08发布

问题:

Here is a 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("whois.internic.net",43);
        *InputStream in = s.getInputStream();
        *OutputStream out = s.getOutputStream();
        String str = (args.length == 0 ? "osborne.com" : args[0] ) + "\n";
        byte buf[] = str.getBytes();
        *out.write(buf);
        System.out.print("hey baby");
        while ((c=in.read()) != -1)
        {
            System.out.print((char) c);
        }
        s.close();
    }
}

I have marked the statements that i have problem understanding.I do not understand what OutputStream object out will hold when it is assigned s.getOutputStream() and what is the need of passing buf to out by out.write(buf) .

I have learned Input and output Streams using files but i do not understand getinputstream and outputstreams.I have googled it ,read it here on SO as well as from many different book and from oracle documents as well . please discuss it in detail .

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 buf array which holds only a string.what i mean to ask is that when in has the input stream of the socket why cant we directly just read from it ? What exactly is a sockets inputstream and outputstream?

I found something here on SO here is the link "Java Networking: Explain InputStream and OutputStream in Socket" ,here an answer by DNA says

In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."

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 "google.com" to outputStream?

回答1:

first thing you need to understand is what is STREAM

A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

****Next is type of streams****

 we have byte stream and character stream.

classes we have in Input Stream and output stream 

as the name suggests in simple terms input stream is used to input the data and output stream is used to output the data

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. also

Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

For reference

  1. What is InputStream & Output Stream? Why and when do we use them?

  2. java DataOutputStream getOutputStream() getInputStream()

Example for getInputStream and getOutputStream

  1. http://zerioh.tripod.com/ressources/sockets.html

New Link http://docs.oracle.com/javase/tutorial/essential/io/buffers.html



回答2:

Here OutputStream is used to send data to other side in socket whenever you write out.write(buf) it will send buffer data in socket.

InputStream is used to receive data from socket.



回答3:

InputStream in and OutputStream out will hold references to two types of streams that you can either read data from or write data to. Don't expect them to hold values from the stream itself - instead, they hold the ability to work with the stream. When you create these objects, you're not sending/receiving any data - you're just getting the object that you can use to send/receive data.

out.write(buf) is sending the contents of buf over the Socket so that any readers of the socket (in your case, in) can receive that data. Whatever data is sent to out will be seen on the other side of the Socket by an InputStream.



标签: java stream