ObjectInputStream.readObject()EOFException

时间:2013-10-25 16:29:48

标签: java objectoutputstream eofexception

我正在尝试使用DatagramSocket从客户端进程向服务器进程发送HashMap,但我得到了ObjectInputStream.readObject() EOFException。我发现有很多关于其他人有这个问题的参考,但我很难搞清楚哪些解决方案适用于我。这是我的代码......

protected static void sendMsgMap(DatagramSocket sendSocket,
                                 DatagramPacket sendPacket,
                                 HashMap<Integer, Boolean> message,
                                 InetAddress address, int port) {
    clearBuffer(sendPacket.getData());
    sendPacket.setAddress(address);
    sendPacket.setPort(port);
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutput outStream = null;
    try {
      outStream = new ObjectOutputStream(byteStream);   
      outStream.writeObject(message);
      outStream.flush();
      outStream.close();
      sendPacket.setData(byteStream.toByteArray());
      sendSocket.send(sendPacket);
    } catch (IOException exception) {
      exception.printStackTrace();
    }
  }

  @SuppressWarnings("unchecked")
  protected static HashMap<Integer, Boolean> receiveMsgMap(DatagramSocket receiveSocket,
                                             DatagramPacket receivePacket) throws SocketTimeoutException {
    HashMap<Integer, Boolean> ret = new HashMap<Integer, Boolean>();
    clearBuffer(receivePacket.getData());
    try {
      receiveSocket.receive(receivePacket);
      ByteArrayInputStream byteStream = new ByteArrayInputStream(receivePacket.getData());
      ObjectInput inStream = new ObjectInputStream(byteStream);
      /* EXCEPTION OCCURS AT LINE BELOW */
      ret = (HashMap<Integer, Boolean>)inStream.readObject();
    } catch (SocketTimeoutException exception) {
      throw exception;
    } catch (IOException | ClassNotFoundException exception) {
      exception.printStackTrace();
    }

    return ret;
  }

这是堆栈跟踪......

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2816)
    at java.io.ObjectInputStream.readHandle(ObjectInputStream.java:1450)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at java.util.HashMap.readObject(HashMap.java:1155)
    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:606)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at info.danforbes.cs370.UDPUtils.receiveMsgMap(UDPUtils.java:94)
    at info.danforbes.cs370.ReliableUDPServer$ServerThread.listen(ReliableUDPServer.java:27)
    at info.danforbes.cs370.UDPServer$ServerThread.run(UDPServer.java:191)
    at info.danforbes.cs370.UDPServer.run(UDPServer.java:106)
    at info.danforbes.cs370.ReliableUDPServer.main(ReliableUDPServer.java:45)

1 个答案:

答案 0 :(得分:2)

查看DataPacket javadoc:http://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html#getData()

并注意偏移和长度