我应该如何处理ServerSocket(Channel).accept()
异常?客户端故障是否会引发此异常?在 JavaDoc 中只说:
IOException - If some other I/O error occurs
我应该如何处理 ServerSocket(Channel).accept() 异常?
关闭频道。如果您应该具有容错能力,请尝试打开一个新的并继续接受。
客户端故障是否会引发此异常?
不。
您如何处理完全取决于您和您的程序流程。我们都看到了来自程序的消息,指出“发生了未知异常”。这很可能是 IOException。它是传输数据可能引起的未知的其他问题。当然,它们还有其他您可以捕获的异常,这些异常更有可能出现并且可以以不同的方式处理。
try {
channel.accept();
} catch(NotYetBoundException e) {
// If this channel's socket has not yet been bound
} catch(ClosedByInterruptException e) {
// If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status
} catch(AsynchronousCloseException e) {
// If another thread closes this channel while the accept operation is in progress
} catch(ClosedChannelException e) {
// If this channel is closed
} catch(SecurityException e) {
// If a security manager has been installed and it does not permit access to the remote endpoint of the new connection
} catch(IOException e) {
// If some other I/O error occurs
}