2

我无法理解一些简单的东西。我有一个处理套接字输入的类。我有一个捕获条款:

    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 子句中被捕获。但是什么也没有发生!它不会捕获套接字异常。我该如何处理?这种行为的原因是什么?

4

1 回答 1

3

要么 SocketExceptionon 不是java.net.Check your imports 中的那个。

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

于 2013-07-15T22:51:17.553 回答