gpt4 book ai didi

java - 套接字异常处理

转载 作者:行者123 更新时间:2023-11-29 05:40:48 24 4
gpt4 key购买 nike

我无法理解一些简单的事情。我有一个处理套接字输入的类。我有一个捕获子句:

    public class EntryPoint implements Runnable {

private Socket socket = null;
private BufferedReader br = null; // receives data from the destination

...

public void run() {
String command = null; // buffer for holding one request from command line
StringReader commandReader = null; // stream for reading command
try {
while (!socket.isClosed() && (command = br.readLine()) != null) {
try {
command = command.trim();
commandReader = new StringReader(command);
Request req = JAXB.unmarshal(commandReader, Request.class);
commandReader.close();
dispatcher.sendRequest(req);
} catch(DataBindingException ex) {
response.sendResponse(SystemMessageFactory.INVALID);
response.sendResponse(SystemMessageFactory.SOCKET_SHUTDOWN);
}
}
} catch (SocketException e) {
System.out.println("Socket Exception");
} catch (IOException e) {
Logger.getLogger("server").log(Level.SEVERE,
"Error reading the command input of the client!", e);
}
}

}

当对等方突然关闭套接字时,将发送连接重置。堆栈跟踪是:

16.07.2013 1:39:51 EntryPoint run
SEVERE: Error reading the command input of the client!
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at blood.steel.server.EntryPoint.run(EntryPoint.java:36)
at java.lang.Thread.run(Thread.java:662)

这怎么可能?我抓了两次! SocketException 被捕获在它自己的 catch 子句和 IOException catch 子句中。但是没有任何反应!它不捕获套接字异常。我该如何处理它,这种行为的原因是什么?

最佳答案

要么 SocketExceptioon 不是 java.net 中的那个。 检查您的导入。

或者您没有运行您认为的代码。

关于java - 套接字异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com