SocketException处理

时间:2013-07-15 22:47:54

标签: java sockets exception

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

    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子句中捕获。但没有任何反应!它没有捕获套接字异常。我该如何处理它以及这种行为的原因是什么?

1 个答案:

答案 0 :(得分:3)

SocketExcepti不是java.net.中的那个检查您的导入。

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