[英]does close a socket will also close/flush the input/output stream
I have a program like that, 我有一个这样的程序,
Socket socket = serverSocket.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
...some read and write here...
socket.close;
The code works fine. 该代码工作正常。 But I am not sure whether the in/out was close if I close the socket or not. 但是我不确定是否关闭插座,输入/输出是否关闭。 Also I didn't call out.flush(), how the data going to be sent out? 我也没有调用out.flush(),如何发送数据?
You should close the outermost OutputStream you have wrapped around the one you got from the socket. 您应该关闭从套接字获得的最外面的OutputStream。 For example: 例如:
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
DataOutputStream dos = new DataOutputStream(bos);
Close 'dos'. 关闭“ dos”。 That flushes it, flushes 'bos', and closes everything. 冲洗它,冲洗“ bos”,然后关闭所有东西。
Flush on close is documented in the Javadoc for FilterOutputStream. JavaOutput中的FilterOutputStream记录了关闭时刷新。
Another answer: In Java, when I call OutputStream.close() do I always need to call OutputStream.flush() before? 另一个答案: 在Java中,当我调用OutputStream.close()时,是否总是需要先调用OutputStream.flush()?
says that yes! 说是的! It will be flushed if you close it manually 如果您手动关闭它将被刷新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.