简体   繁体   English

java DataOutputStream getOutputStream()getInputStream()

[英]java DataOutputStream getOutputStream() getInputStream()

one question 一个问题

in the case for example of 例如

DataOutputStream output= new DataOutputStream(clientSocket.getOutputStream()) ;

or 要么

DataInputStream in = new   DataInputStream(clientSocket.getInputStream());

must these objects to be created each time i need an I/O operation or just invoke a read or a write on them each time i need??? 每当我需要I / O操作时是否必须创建这些对象,或者每次我需要时对它们进行读取或写入? ( plus some flushing after each operaration) (加上每次操作后的一些冲洗)

您只能创建一次这些对象,也就是在初始化套接字之后。

Both variants are possible, but it is more useful to create them only once. 两种变体都是可能的,但是只创建一次就更有用。

If you want some buffering (to avoid sending a new TCP packet for each write command), you may want to think about putting a BufferedInputStream between the Socket and DataIn/Output: 如果要进行一些缓冲(以避免为每个写命令发送新的TCP数据包),则可能需要考虑在Socket和DataIn / Output之间放置一个BufferedInputStream:

DataOutput output = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
DataInput input   = new DataInputStream (new BufferedInputStream (clientSocket.getInputStream()));

I'm using the interfaces DataInput/DataOutput instead of the Stream classes here, since often you'll only need the methods defined there. 我在这里使用的是DataInput / DataOutput接口而不是Stream类,因为通常您只需要在那里定义的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM