1
即時得到此異常反序列化對象:控制檯序列化反序列化對象EOFException類
sending request: GET_OBJS
java.io.EOFException
receiving response
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at java.io.ObjectStreamClass.readNonProxy(Unknown Source)
at java.io.ObjectInputStream.readClassDescriptor(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at thesis.bot.wab.applet.DApplet.extracted(DApplet.java:218)
我試圖反序列化對象沒有使用文件上
輸出。該對象使用DatagramSocket發送。
從一個Java小應用程序調用我:
sendPacket("GET_OBJS");
[...]
input=new ObjectInputStream(new ByteArrayInputStream(receivedPacket.getData()));
objs=extracted(input);
toWrite=Integer.toString(objs.size());
input.close();
sendPacket("PRINT_"+toWrite);
[...]
private LinkedList<Objects> extracted(ObjectInputStream obj)
throws IOException, ClassNotFoundException {
return (LinkedList<Objects>) obj.readObject();
}
private void sendPacket(String msg) throws IOException{
System.out.println("sending request: "+msg);
buf=msg.getBytes();
DatagramPacket tosend=new DatagramPacket(buf,buf.length,addr,9999);
socket.send(tosend);
}
在另一側:
else if(req.compareTo("GET_OBJS")==0){
Environement env=(Environement) ctx.getService(ctx.getServiceReference(Environement.class.getName()));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream(baos);
out.writeObject(env.getObjects());
out.close();
buf=baos.toByteArray();
resp="buffer_ready";
}
其中env.getObjects()返回的「對象和對象一個LinkedList是Serializable類
有什麼問題?
數據報不能保證到達目的地,只有它會嘗試。你爲什麼不使用TCP? – SJuan76
你可以看到這個相關的帖子http://stackoverflow.com/questions/3997459/send-and-receive-serialize-object-on-udp-in-java – xhamr
最可能的原因是數據流已經截斷。 –