Howto send objects between a client and a server

Discussion in 'Java' started by Brian, Oct 10, 2007.

  1. Brian

    Brian Guest

    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();
    ****************************
    Brian, Oct 10, 2007
    #1
    1. Advertising

  2. On Wed, 10 Oct 2007 11:13:37 +0200, Brian wrote:
    > 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

    --
    Gordon Beaton, Oct 10, 2007
    #2
    1. Advertising

  3. Brian

    Roedy Green Guest

    On Wed, 10 Oct 2007 11:13:37 +0200, Brian <mail@DEL_oddball_ETE.dk>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Oct 10, 2007
    #3
  4. Brian

    Brian Guest

    On 10 Oct 2007 10:02:39 GMT, Gordon Beaton <> wrote:

    >
    >MyObject any other objects it holds (except those marked "transient")
    >needs to "implements Serializable".


    Thx
    that solved the problem :)

    /Brian
    Brian, Oct 10, 2007
    #4
  5. Brian

    Lew Guest

    Brian wrote:
    > On 10 Oct 2007 10:02:39 GMT, Gordon Beaton <> wrote:
    >
    >> 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.

    --
    Lew
    Lew, Oct 11, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    3,333
    Peter
    Jul 1, 2003
  2. OpenPavilion
    Replies:
    3
    Views:
    329
    Lawrence D'Oliveiro
    Aug 13, 2007
  3. schnitzell

    How to send whole file between server's and client's side?

    schnitzell, Aug 29, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    125
    Michael Nemtsev
    Aug 30, 2005
  4. Roberto
    Replies:
    3
    Views:
    297
  5. Dan Sikorsky

    HOWTO: Send Secure Data To Client Using SSL

    Dan Sikorsky, Oct 17, 2003, in forum: ASP General
    Replies:
    3
    Views:
    149
    Ray at
    Oct 18, 2003
Loading...

Share This Page