java DataOutputStream getOutputStream() getInputStream()
一个问题
是,例如
DataOutputStream output= new DataOutputStream(clientSocket.getOutputStream()) ;
,
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
每次我需要 I/O 操作时必须创建这些对象,还是每次我需要时都调用它们的读取或写入??? (每次操作后加上一些冲洗)
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??? ( plus some flushing after each operaration)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能创建这些对象一次,即在套接字初始化之后。
You must create these objects only once, that is, after your socket has been initialized.
两种变体都是可能的,但只创建一次更有用。
如果您想要一些缓冲(以避免为每个写入命令发送新的 TCP 数据包),您可能需要考虑在 Socket 和 DataIn/Output 之间放置 BufferedInputStream:
我使用接口 DataInput/DataOutput 而不是 Stream 类在这里,因为通常您只需要那里定义的方法。
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:
I'm using the interfaces DataInput/DataOutput instead of the Stream classes here, since often you'll only need the methods defined there.