1

我正在尝试使用 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)
4

1 回答 1

2

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

并注意偏移量和长度

于 2013-10-25T16:38:19.463 回答