2013-10-17 69 views
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類

有什麼問題?

+0

數據報不能保證到達目的地,只有它會嘗試。你爲什麼不使用TCP? – SJuan76

+0

你可以看到這個相關的帖子http://stackoverflow.com/questions/3997459/send-and-receive-serialize-object-on-udp-in-java – xhamr

+0

最可能的原因是數據流已經截斷。 –

回答

2

我修改了使用tcp套接字的代碼,現在我沒有gett例外。

修改後的代碼:

從Java小程序我叫:

sendPacket("GET_OBJS");     
[...] 
    ObjectInputStream ois=new ObjectInputStream(socket.getInputStream()); 
    objs=extracted(ois); 
    System.out.println(objs.size()); 
[...] 

private LinkedList<Objects> extracted(ObjectInputStream ois) 
     throws IOException, ClassNotFoundException { 
    return (LinkedList<Objects>) ois.readObject(); 
} 

private void sendPacket(String msg) throws IOException{ 
    bw.write(msg); 
    bw.newLine(); 
    bw.flush(); 
    System.out.println("Message sent"); 
} 

的另一面:

else if(req.compareTo("GET_OBJS")==0){ 
     Environement env=(Environement) ctx.getService(ctx.getServiceReference(Environement.class.getName())); 
     ObjectOutputStream out=new ObjectOutputStream(clientSock.getOutputStream()); 
     out.flush();    
     out.writeObject(env.getObjects()); 
     out.flush(); 
     resp="buffer_ready"; 
    } 

現在,它的作品! 謝謝你的幫助!

PS:在開始寫入之前,ObjectOutputStream需要調用flush()。