[英]Understanding getInputStream and getOutputStream
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)
. 我有显着的,我有问题的理解。我不明白的OutputStream对象的陈述
out
将举行当它被分配s.getOutputStream()
什么是合格的需要buf
到out
由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 . 我已经学会了使用文件输入和输出Streams,但我不了解
getinputstream
和outputstreams
。我已经google了它,在SO上以及从许多不同的书和oracle文档中读取它。 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 ? 我知道如何从文件中读取以及如何写入它们。但是在这里我不明白需要传递只保存字符串的
buf
数组。我想问的是当in有插槽的输入流时为什么我们不能直接读它呢? What exactly is a sockets inputstream
and outputstream
? 什么是套接字输入
inputstream
和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 我在这里发现了一些东西,这里是链接“ Java Networking:在Socket中解释InputStream和OutputStream ”,这里的答案由DNA说
In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."
在Java中,要通过套接字发送数据,可以从中获取OutputStream(1),并写入OutputStream(输出一些数据)。“
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? 这让我感到困惑,当outputStream用于通过socket发送数据时需要out.write(buf)为什么我们需要将“google.com”发送到outputStream?
first thing you need to understand is what is STREAM 您需要了解的第一件事是什么是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.
InputStream用于从源读取数据,OutputStream用于将数据写入目标。
****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. Java 字节流用于执行8位字节的输入和输出。 Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream.
尽管有许多与字节流相关的类,但最常用的类是FileInputStream和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. Java 字节流用于执行8位字节的输入和输出,其中Java 字符流用于执行16位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.
虽然有许多与字符流相关的类,但最常用的类是FileReader和FileWriter ..虽然内部FileReader使用FileInputStream而FileWriter使用FileOutputStream但是主要区别在于FileReader一次读取两个字节而FileWriter写入两个字节一时间
For reference 以供参考
What is InputStream & Output Stream? 什么是InputStream和输出流? Why and when do we use them?
我们为何以及何时使用它们?
java DataOutputStream getOutputStream() getInputStream() java DataOutputStream getOutputStream()getInputStream()
Example for getInputStream and getOutputStream getInputStream和getOutputStream的示例
New Link http://docs.oracle.com/javase/tutorial/essential/io/buffers.html 新链接 http://docs.oracle.com/javase/tutorial/essential/io/buffers.html
InputStream in
and OutputStream out
will hold references to two types of streams that you can either read data from or write data to. InputStream in
和OutputStream out
将保存对两种类型流的引用,您可以从中读取数据或将数据写入。 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. out.write(buf)
正在发送的内容buf
在插座,以便在插槽的任何读者(你的情况, in
)可以接收数据。 Whatever data is sent to out
will be seen on the other side of the Socket by an InputStream
. 任何数据被发送到
out
将在插座的另一侧由可见InputStream
。
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. 这里
OutputStream
用于在写出时将数据发送到套接字的另一端。写out.write(buf)
它将在套接字中发送缓冲区数据。
InputStream
is used to receive data from socket. InputStream
用于从套接字接收数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.