[英]Java OutputStream and InputStream initialization
I have the following code: 我有以下代码:
private Socket mSenderSocket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
...
try
{
mSenderSocket = new Socket(java.net.InetAddress.getByName(TCP_SERVER_IP), 12345);
out = new DataOutputStream(mSenderSocket.getOutputStream());
in = new DataInputStream(mSenderSocket.getInputStream());
}
catch (UnknownHostException ex)
{
System.err.println("Don't know about host.");
}
catch(IOException ex)
{
System.err.println("Couldn't get I/O");
}
I am interested in the following scenario: 我对以下场景感兴趣:
During the chat session if an I/O exception occurs then I have to do the following: 在聊天会话期间,如果发生I / O异常,那么我必须执行以下操作:
mSenderSocket = new Socket(java.net.InetAddress.getByName(TCP_SERVER_IP);
Now what happens with the DataInputStream
and the DataOutputStream
? 现在
DataInputStream
和DataOutputStream
会发生什么?
Should I initialize theese objects again or not? 我应该再次初始化theese对象吗?
Almost certainly in an error condition you want to throw away the entire object and start again, or don't even construct the object in the first place. 几乎可以肯定,在错误情况下,您想要丢弃整个对象并重新开始,或者甚至不首先构造对象。
(You also probably want a finally
on that to close the Socket
. As it happens, calling close
on either stream or on the Socket
will completely close all three objects.) (你也可能想要
finally
关闭Socket
。当它发生时,在任一流或Socket
上调用close
将完全关闭所有三个对象。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.