Howto send objects between a client and a server

B

Brian

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();
****************************
 
G

Gordon Beaton

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 :)

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

--
 
L

Lew

Brian said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top