user1156544
user1156544

Reputation: 1817

I/O Socket error (Java)

I have a problem using a socket:

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

I have checked "is" is not null (with println): java.net.SocketInputStream@403a7c5e

The IOexception message is: 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)

Does anybody know what is going on? Thanks.

EDIT: The object is an ObjectInputStream

Upvotes: 0

Views: 1582

Answers (2)

user207421
user207421

Reputation: 311018

To expand on Eli's answer a bit, the input stream is not only not a valid ObjectInputStream produced by an ObjectOutputStream at the peer, it is also empty. The peer has closed the connection without instantiating an ObjectOutputStream on it at all, or doing any I/O whatsoever.

Upvotes: 0

Eli Acherkan
Eli Acherkan

Reputation: 6411

My first guess is that the input stream you're getting is not a valid object stream. An ObjectInputStream is recognized by a particular stream header.

The important thing though is not only to understand this exception, but to learn how to analyze exceptions and failures. I suggest the following:

  1. Examine your program's output (log files or console). See if it shows the exact type of exception and the full stack trace.
  2. Use a debugger. Put a breakpoint at the problematic line, stop there with a debugger, and then step into the code until you get to the exception.

EDIT:

The stack trace shows that the ObjectInputStream tries to read a short value from the input stream, but the stream doesn't have any more data in it. Sounds like the server isn't sending the data you're expecting. (Or it simply isn't sending any data at all.)

In order to analyze the data you're receiving from the socket, you can temporarily change the code so that it reads from the input stream and dumps the contents (e.g. to System.out). Or you can use a network sniffer (such as Wireshark) that will show you all the network traffic between your machine and the server.

Upvotes: 1

Related Questions