[問題] 用socket傳object (done)
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
→
12/03 19:24, , 1F
12/03 19:24, 1F
推
12/03 19:26, , 2F
12/03 19:26, 2F
→
12/03 19:34, , 3F
12/03 19:34, 3F
→
12/03 19:57, , 4F
12/03 19:57, 4F
→
12/03 19:58, , 5F
12/03 19:58, 5F
※ 編輯: InitialShuk 來自: 140.115.222.4 (12/03 20:12)
→
12/03 20:22, , 6F
12/03 20:22, 6F
→
12/03 20:29, , 7F
12/03 20:29, 7F
推
12/04 17:03, , 8F
12/04 17:03, 8F
→
12/04 17:05, , 9F
12/04 17:05, 9F