Hi
I'm trying to write a little program that can send an object between a
client and a server - and back. It won't work and I get theese
exceptions:
Server:
java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: MyObject
Client:
java.io.NotSerializableException: MyObject
I need a little hint :)
/Brian
The server:
****************************
ServerSocket ss = new ServerSocket(port);
Socket con = ss.accept();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
MyObject my = (MyObject) in.readObject();
ObjectOutputStream out = new
ObjectOutputStream(con.getOutputStream());
out.writeObject(my);
out.flush();
con.close();
****************************
The client:
**************************
Socket con = new Socket("localhost", port);
ObjectOutputStream out = new
ObjectOutputStream(con.getOutputStream());
out.writeObject(new MyObject());
out.flush();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
MyObject my = (MyObject) in.readObject();
System.out.println(my.toString());
con.close();
****************************
Gordon Beaton - 10 Oct 2007 11:02 GMT
> I'm trying to write a little program that can send an object between a
> client and a server - and back. It won't work and I get theese
[quoted text clipped - 8 lines]
>
> I need a little hint :)
MyObject any other objects it holds (except those marked "transient")
needs to "implements Serializable".
Here's another tip: make a habit of always creating the
ObjectOutputStream *before* the ObjectInputStream. Your server does it
the other way around, and that could come back to bite you one day.
Lots more here:
http://java.sun.com/javase/6/docs/technotes/guides/serialization/index.html
/gordon
--
Brian - 10 Oct 2007 15:07 GMT
>MyObject any other objects it holds (except those marked "transient")
>needs to "implements Serializable".
Thx
that solved the problem :)
/Brian
Lew - 11 Oct 2007 07:47 GMT
>> MyObject any other objects it holds (except those marked "transient")
>> needs to "implements Serializable".
>
> Thx [sic]
> that solved the problem :)
and created new ones.
Simply marking a class Serializable doesn't cover all bases. There's a lot of
work to making a class properly Serializable. Study up on it.

Signature
Lew
Roedy Green - 10 Oct 2007 12:39 GMT
>java.io.WriteAbortedException: writing aborted;
>java.io.NotSerializableException: MyObject
see http://mindprod.com/jgloss/serialization.html
Sounds like you are trying to write an object that does not implement
Serializable, or that points (possibly indirectly) to objects (that
will be dragged along with the write) that don't.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com