【问题标题】:SocketException Handling套接字异常处理
【发布时间】:2013-07-13 23:09:39
【问题描述】:

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

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

【问题讨论】:

  • 这是在帮您一个忙。打印固定文字不足以作为记录异常的方法。记录实际的异常,而不仅仅是您自己的一些消息。 isClosed() 测试毫无意义。 readLine() 返回 null 是正确的测试。您应该关闭套接字并在捕获除 SocketTimeoutException 之外的任何 IOException 时中断,也可能如此,具体取决于您的要求。
  • 堆栈跟踪在那里。它甚至不应该出现,不是吗?
  • 我是评论,不是回答。

标签: java sockets exception


【解决方案1】:

SocketExceptioon 不是java.net. 中的一个,请检查您的导入。

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

【讨论】:

  • java.net.* - 我刚刚检查过。如果我在 Eclipse 中将其作为一个整体应用程序启动,如何运行其他代码? Socket的InputStream从这个类中是只读的。
  • 天啊,谢谢。这很愚蠢。我更改了它并忘记停止进程 =) 然而,为什么它没有被 IOException 子句捕获?只有当我明确声明 SocketException 子句时才会捕获它。
  • 由于您没有运行该代码,因此您不能期望它执行 任何
  • 是的。现在它运行了。多谢你们。愚蠢的错误=)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多