2012-07-28 31 views
0

我有我的客戶下面的方法:爲什麼readBoolean塊但readObject不?

public boolean save() { 
    this.ostream.writeObject(Command.SAVE); // write to socket 
    return this.istream.readBoolean(); 
    } 

在我的服務器:

Object o = this.istream.readObject(); 
    if (o == Command.SAVE) { 
     boolean isSaved = save(); // save to database 
     this.ostream.writeBoolean(isSaved); 
    } 

如果我使用readBooleanwriteBoolean,該readBoolean方法在客戶端塊,但如果我使用readObject和相反,我的應用程序工作正常。爲什麼readBoolean方法會阻止是否有原因?

+0

我認爲阻止不是正確的詞。兩者都阻塞。問題是爲什麼一個人不回來? – Jus12 2016-10-18 00:49:57

回答

0

它更像發表評論,但代碼示例將是很難看,所以我張貼作爲一個答案...希望它不會打擾任何人

你確定?我剛剛通過此代碼對其進行了測試

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("d:/objects.obj")); 
System.out.println("writeObject(true)"); 
out.writeObject(true); 
System.out.println("writeBoolean(true)"); 
out.writeBoolean(true); 
System.out.println("writeBoolean(true)"); 
out.writeBoolean(true); 
out.close();//or flush 
System.out.println("------------------"); 

ObjectInputStream in=new ObjectInputStream(new FileInputStream("d:/objects.obj")); 
System.out.println("readObject="+in.readObject()); 
System.out.println("readBoolean="+in.readBoolean()); 
System.out.println("readObject="+in.readObject());//<- readObject stored by writeBoolean 

它的接縫工作正常。唯一的問題是當我試圖readObject存儲與writeBoolean或反之亦然readBooleanwriteObject存儲。

也許你只需要刷新輸出流。

+0

我刷新了輸出流,它工作!謝謝! – 2012-09-12 06:16:32

+0

我得到了同樣的問題,並沖洗了伎倆。 – 2016-04-22 23:04:17