Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Howto send objects between a client and a server

Thread view: 
Brian - 10 Oct 2007 10:13 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.