我收到了一個監聽器,它監聽數據。現在的問題是發送數據的客戶端最終會自行關閉連接。基於我的代碼下面,我想知道我是否仍然需要執行這部分代碼,它在writeBuffer.close();
?客戶端關閉連接
我應該刪除最後一部分,只是把套接字關閉捕獲?
public void run()
{
BufferedWriter writeBuffer = null;
BufferedReader readBuffer = null;
String message="";
try {
writeBuffer = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
readBuffer = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
int m = 0, count=0;
int nextChar=0;
while ((nextChar=readBuffer.read()) != -1)
{
message += (char) nextChar;
if (nextChar == '#')
{
System.out.println("\n\nSending PA : "+message);
writeBuffer.write("$PA\r\n");
writeBuffer.flush();
message="";
}
}
}
catch (Exception ex)
{
System.out.println("MyError:Exception has been caught in in the main first try");
ex.printStackTrace(System.out);
}
/*finally
{
try
{
if (writeBuffer != null)
{
writeBuffer.close();
}
else
{
System.out.println("MyError:writeBuffer is null in finally close");
}
}
catch(IOException ex)
{
ex.printStackTrace(System.out);
}
}*/
}
交叉發佈到Oracle Java論壇:https://forums.oracle.com/forums/thread.jspa?threadID=2314932&tstart=0。 – EJP