看板 java 關於我們 聯絡資訊
server先執行 在client server: public class s { public static void main(String[] args) { try { ServerSocket _port=new ServerSocket(9876); Socket _sock=_port.accept(); ObjectInputStream _ois=new ObjectInputStream(_sock.getInputStream()); System.out.println(_ois.readUTF()); Object _one=_ois.readObject(); System.out.println("e"); person _per=(person) _one; System.out.println(_per.getID()); } catch (Exception e) {e.printStackTrace();} } public class person implements Serializable{/*跟client的一起貼在下面*/} } client: public class c implements Serializable{ <===註一 public static void main(String[] args) { new c().go();} public void go(){ try { Socket _sock=new Socket("127.0.0.1",9876); ObjectOutputStream _oos=new ObjectOutputStream(_sock.getOutputStream()); person _per=new person("111","222","333","444"); _oos.writeUTF("123"); _oos.flush(); _oos.writeObject(_per); _oos.flush(); } catch (IOException e) {e.printStackTrace();} } public class person implements Serializable{/*跟server的一起貼在下面*/} } 各自有相同的class :person (getter setter省略) public class person implements Serializable{ private static final long serialVersionUID = 1L; public String ID; public String name; public String PN; public String Email; public person(String _temp1,String _temp2,String _temp3,String _temp4){ this.ID=_temp1; this.name=_temp2; this.PN=_temp3; this.Email=_temp4; } } ----------------------- 原先是javaNoSerializableException (在server readobject時) 再多加了"註一"後 變成了classcastexception 問一 看過許多範例 不是傳送的物件序列化就好? 問二 加了註一後 為啥不能解序列化? -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.222.4
InitialShuk:setter getter因為排版刪掉了 12/03 19:24
PsMonkey:不負責任意見:你有沒有 args 的 constructor 嗎? 12/03 19:26
InitialShuk:Client or server? 還是person不太懂你講的 12/03 19:34
tkcn:看程式碼似乎沒問題...倒是你最後的結論不知在寫啥 12/03 19:57
InitialShuk:冏 我看我把完整的CODE貼出來好了~"~ 12/03 19:58
※ 編輯: InitialShuk 來自: 140.115.222.4 (12/03 20:12)
tkcn:person 該不會是 inner class 吧 (暈) 12/03 20:22
InitialShuk:感謝>< 12/03 20:29
walm20:建議 像c++板 10/23 tiyun □ [公告] 張貼程式碼 12/04 17:03
walm20:貼在文章內 看得有點痛苦 12/04 17:05