I / O套接字错误(Java)

时间:2012-03-13 15:56:18

标签: java sockets io ioexception

使用套接字时遇到问题:

        socket = new Socket(hostname, port);
        os = socket.getOutputStream();
        oos = new ObjectOutputStream(os);
        is = socket.getInputStream();
        ois = new ObjectInputStream(is);  // THIS ALWAYS RAISES EXCEPTION

我检查过“is”不为null(使用println): java.net.SocketInputStream@403a7c5e

IOexception消息是:null

java.io.EOFException
        at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2297)
        at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2766)
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:797)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:297)
        at com.my.Comunication.sendData(Netw.java:140)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:246)
        at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)
        at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:257)
        at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:95)
        at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:629)
        at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:588)
        at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:573)
        at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:470)
        at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:295)
        at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:515)
        at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:285)
        at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:143)
        at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:155)
        at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:189)
        at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:76)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:636)

有人知道发生了什么吗?感谢。

编辑:对象是ObjectInputStream

2 个答案:

答案 0 :(得分:1)

我的第一个猜测是您获得的输入流不是有效的对象流。 ObjectInputStream由特定的流标头识别。

重要的是,不仅要了解这个例外,还要学习如何分析异常和失败。我建议如下:

  1. 检查程序的输出(日志文件或控制台)。看看它是否显示了确切的异常类型和完整的堆栈跟踪。
  2. 使用调试器。在有问题的行中放置一个断点,使用调试器停在那里,然后单步执行代码,直到遇到异常。
  3. 修改

    堆栈跟踪显示ObjectInputStream尝试从输入流中读取short值,但该流中不再包含任何数据。听起来服务器没有发送您期望的数据。 (或者它根本就不发送任何数据。)

    为了分析您从套接字接收的数据,您可以临时更改代码,以便从输入流中读取并转储内容(例如,转移到System.out)。或者您可以使用网络嗅探器(例如Wireshark)来显示机器和服务器之间的所有网络流量。

答案 1 :(得分:0)

为了扩展Eli的答案,输入流不仅不是对等体ObjectInputStream产生的有效ObjectOutputStream,它也是空的。对等体已经关闭了连接而没有在其上实例化ObjectOutputStream,或者做任何I / O.