writeObject and readObject problem

Discussion in 'Java' started by juicy, Sep 21, 2004.

  1. juicy

    juicy Guest

    i very blur with the use of writeObject and readObject,

    here's the client,
    if (b.getLabel() == "write") {
    try{
    oos.writeObject((Object)event);}//event is button pressed event
    oos.flush();
    System.out.println("write event");
    catch(Exception e){}

    if(b.getLabel() == "Read"){
    Event get=(Event)iis.readObject();
    iis.close();
    System.out.println("read event");

    and here's the server

    try{
    ois = new ObjectInputStream(socket.getInputStream());
    oos = new ObjectOutputStream(socket.getOutputStream());}

    i try to send the button pressed event, the writeObject is ok but the
    readObject is blocked. Is the event can't be sent like this? I am just
    learning to do that. At server, i need do a function to forward the object
    or not?
     
    juicy, Sep 21, 2004
    #1
    1. Advertising

  2. On Tue, 21 Sep 2004 05:47:27 -0400, juicy wrote:
    > i try to send the button pressed event, the writeObject is ok but
    > the readObject is blocked. Is the event can't be sent like this? I
    > am just learning to do that.


    readObject() will block until the server actually writes an object to
    the stream for the client to read. Has it written an object?

    > At server, i need do a function to forward the object or not?


    You have created two *independent* object streams, one from the client
    to the server, and one from the server to the client:

    client server
    stream 1: ObjectOutputStream --> ObjectInputStream
    stream 2: ObjectInputStream <-- ObjectOutputStream

    Stream 1 and stream 2 are not connected to each other! If the client
    writes an object to the server, that object is *not* automatically
    sent back to the client. Is that what you are trying to do?

    When the client writes an object to his ObjectOutputStream, the server
    can read it from his ObjectInputStream. If the server writes an object
    to his ObjectOutputStream, the client can read it from his
    ObjectInputStream.

    Note: it is a good idea to always create the ObjectOutputStream before
    the ObjectInputStream (you did not do this in your server).

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 21, 2004
    #2
    1. Advertising

  3. juicy

    newton klea Guest

    from server you have to do exactly what you do on the client but
    reverse the call sequences. eg:

    ois = new ObjectInputStream(req.getInputStream());
    Object o = ois.readObject();
    // do something with your object o and send back:
    oos = new ObjectOutputStream(res.getOutputStream());
    oos.writeObject(output);
    oos.flush();

    this example use http server req and res but you can use socket in
    place.
     
    newton klea, Sep 21, 2004
    #3
  4. juicy

    juicy Guest

    i have modified the server like below,

    class Connect extends Thread {
    private Socket client = null;
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;

    public Connect(Socket clientSocket) {
    client = clientSocket;
    try {
    ois = new ObjectInputStream(client.getInputStream());
    oos = new ObjectOutputStream(client.getOutputStream());
    } catch(Exception e1) {
    try {
    client.close();
    }catch(Exception e) {
    System.out.println(e.getMessage());
    }
    return;
    }
    this.start();
    }

    public void run() {
    Object x = null;

    try {
    x = ois.readObject();
    ois.close();

    oos.writeObject(x);
    oos.flush();

    oos.close();
    client.close();
    } catch(Exception e) {System.out.println(e.getMessage());}//an
    exception message is printed out here
    }
    }

    i get an exception message :
    Descriptor not a socket: socket write error
     
    juicy, Sep 22, 2004
    #4
  5. juicy

    juicy Guest

    I have modified the server like below,
    class Connect extends Thread {
    private Socket client = null;
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;

    public Connect() {}

    public Connect(Socket clientSocket) {
    client = clientSocket;
    try {
    ois = new ObjectInputStream(client.getInputStream());
    oos = new ObjectOutputStream(client.getOutputStream());
    } catch(Exception e1) {
    try {
    client.close();
    }catch(Exception e) {
    System.out.println(e.getMessage());
    }
    return;
    }
    this.start();
    }

    public void run() {
    Object x = null;

    try {
    x = ois.readObject();
    ois.close();

    oos.writeObject(x);
    oos.flush();

    oos.close();
    client.close();
    } catch(Exception e) {System.out.println(e.getMessage()); //an
    exception message is printed out here
    }
    }
    }

    i get an message:
    Descriptor not a socket: socket write error
    what cause the error occur? How to solve it?
     
    juicy, Sep 22, 2004
    #5
  6. On Tue, 21 Sep 2004 23:15:35 -0400, juicy wrote:
    > try {
    > x = ois.readObject();
    > ois.close();
    >
    > oos.writeObject(x);
    > oos.flush();
    >
    > oos.close();
    > client.close();


    [...]
    >
    > i get an message:
    > Descriptor not a socket: socket write error
    > what cause the error occur? How to solve it?


    When you close the ObjectInputStream, it closes the underlying
    SocketInputStream and ultimately the Socket itself. Your subsequent
    attempt to write to the ObjectOutputStream encounters a closed Socket.

    So don't close either of the streams until you have finished
    communicating with that client.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 22, 2004
    #6
  7. juicy

    juicy Guest

    ok..
    after the server write the object, i get the object at client like below

    if(b.getLabel()=="Connect")
    {
    try{
    Socket socket=new Socket(host, port);
    oos = new ObjectOutputStream(socket.getOutputStream());
    ois = new ObjectInputStream(socket.getInputStream());
    }
    catch(Exception e){}
    }

    if(b.getLabel()=="Write"){
    try{
    oos.writeObject((Object)event);}
    catch(Exception e){}
    }

    if(b.getLabel()=="Read"){
    try{
    Event get=(Event)ois.readObject();
    oos.close();
    ois.close();}
    catch(IOException e){}
    catch(ClassNotFoundException e){}//an exception occured here

    The client still can't read the object and ClassNotFoundException
    occurred.
    What's wrong with my code this time?
     
    juicy, Sep 22, 2004
    #7
  8. On Wed, 22 Sep 2004 03:16:00 -0400, juicy wrote:

    > catch(IOException e){}
    > catch(ClassNotFoundException e){}//an exception occured here


    This is very shoddy practice.
    <http://www.physci.org/codes/javafaq.jsp#stacktrace>

    ...
    > The client still can't read the object and ClassNotFoundException
    > occurred.
    > What's wrong with my code this time?


    <http://www.physci.org/codes/javafaq.jsp#exact>

    Which has some hand tips, and points to here..

    <http://mindprod.com/jgloss/runerrormessages.html#INDEX>
    <http://mindprod.com/jgloss/runerrormessages.html#CLASSNOTFOUNDEXCEPTION>

    Did you import java.awt.Event in your server?

    One more thing, you might find people can help you
    a lot more easily if you provide an SSCCE
    <http://www.physci.org/codes/sscce.jsp>

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
     
    Andrew Thompson, Sep 22, 2004
    #8
  9. juicy

    juicy Guest

    I am using Microsoft VM instead of JVM, is the exception because of no
    certain class in Microsoft VM?
     
    juicy, Sep 22, 2004
    #9
  10. Michael Borgwardt, Sep 22, 2004
    #10
  11. On Wed, 22 Sep 2004 04:40:46 -0400, juicy wrote:

    > I am using Microsoft VM instead of JVM, is the exception because of no
    > certain class in Microsoft VM?


    I think the stacktrace and SSCCE will best determine that.

    OTOH, if you have a self-contained piece of code <10Kb,
    you can check it against the 1.1 VM (either 1.1.8[1] or
    specifically the MSVM[2]) at the Java On-Line Compiler.

    [1] <http://www.physci.org/javac.jsp?bcp=11>
    [2] <http://www.physci.org/javac.jsp?bcp=MS>

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
     
    Andrew Thompson, Sep 22, 2004
    #11
  12. juicy

    juicy Guest

    So terrible i get message like this
    java.lang.ClassNotFoundException
    at java/io/ObjectInputStream.loadClass0
    at java/io/ObjectInputStream.resolveClass
    at java/io/ObjectInputStream.readClassDescriptor
    at java/io/ObjectInputStream.readNewObject
    at java/io/ObjectInputStream.readObject
    at java/io/ObjectInputStream.goToEndOfBlockData
    at java/io/ObjectInputStream.readNewObject
    at java/io/ObjectInputStream.readObject
    at com/ms/!!!Internal_Class_0.DefaultReadMethod
    at java/io/ObjectInputStream.invokeDefaultReadObject
    at java/io/ObjectInputStream.defaultReadObject
    at java/io/ObjectInputStream.readNewObject
    at java/io/ObjectInputStream.readObject
    at MainVirtual.run
    at java/lang/Thread.run

    :( What should i do now?
     
    juicy, Sep 23, 2004
    #12
  13. On Thu, 23 Sep 2004 04:45:40 -0400, juicy wrote:
    > So terrible i get message like this
    > java.lang.ClassNotFoundException
    > at java/io/ObjectInputStream.loadClass0
    > at java/io/ObjectInputStream.resolveClass
    > at java/io/ObjectInputStream.readClassDescriptor
    > at java/io/ObjectInputStream.readNewObject
    > at java/io/ObjectInputStream.readObject
    > at java/io/ObjectInputStream.goToEndOfBlockData
    > at java/io/ObjectInputStream.readNewObject
    > at java/io/ObjectInputStream.readObject
    > at com/ms/!!!Internal_Class_0.DefaultReadMethod
    > at java/io/ObjectInputStream.invokeDefaultReadObject
    > at java/io/ObjectInputStream.defaultReadObject
    > at java/io/ObjectInputStream.readNewObject
    > at java/io/ObjectInputStream.readObject
    > at MainVirtual.run
    > at java/lang/Thread.run
    >
    :mad: What should i do now?


    The process that did ObjectInputStream.readObject() doesn't have
    access to a class definition that corresponds to the received object.
    For example, you can't send an object "foo" of class MyClass to the
    server, unless the server knows what MyClass is.

    If the server doesn't need to actually *do* anything with the object
    except immediately resend it on the ObjectOutputStream, let it read
    the raw bytes from the SocketInputStream instead, and resend them on
    the SocketOutputStream. That way it never has to create the object
    itself.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 23, 2004
    #13
  14. juicy

    juicy Guest

    To gordon,
    Yes, you are right. I finally know the main problem is because i try to
    send a button event to server. I don't know why the server can read the
    event which is sent by client but when it resend it on the
    SocketOutputStream, the client can't read it back.
    I try to change the object as string instead of a button event, it works
    well. Is that i can't send event like this way? or..
    Does anyone can help me??
     
    juicy, Sep 23, 2004
    #14
  15. On Thu, 23 Sep 2004 10:33:56 -0400, juicy wrote:

    > Yes, you are right. I finally know the main problem is because i try to
    > send a button event to server. I don't know why the server can read the
    > event which is sent by client


    No, it cannot! Look at the stacktrace closely..

    java.lang.ClassNotFoundException
    at java/io/ObjectInputStream.loadClass0
    .....
    at java/io/ObjectInputStream.readObject

    It throws the exception when it tries to *read*
    the object it is given. Your server cannot even
    read the object, Gordon has suggested one anser,
    but I will suggest another.

    In your server code, top line..

    import java.awt.Event;

    Your server does not need it till run time,
    that is why your code compiles, but to recognize
    an Event you are reading, the java.awt.Event
    must be imported to the server code.

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
     
    Andrew Thompson, Sep 23, 2004
    #15
  16. Andrew Thompson wrote:
    > In your server code, top line..
    >
    > import java.awt.Event;
    >
    > Your server does not need it till run time,
    > that is why your code compiles, but to recognize
    > an Event you are reading, the java.awt.Event
    > must be imported to the server code.


    That can't be right. Java's import is nothing but a compiler
    directive to allow the use of classnames without prepending the
    package. It *cannot* have any influence whatsoever on runtime
    behaviour. In compiled code and in serialized objects, all
    classes are referred by their fully qualified name.

    The only thing he needs is java.awt.Event in the server's
    classpath, which should be no problem since it has been part
    of the API since 1.1.
     
    Michael Borgwardt, Sep 23, 2004
    #16
  17. juicy

    juicy Guest

    Yes, i have imported java.awt.Event but the result is still same.
    Is that really the server cannot read the event?
    //at server
    public void run() {
    Object x = null;
    try {
    x = ois.readObject();
    System.out.println("print out what is received: "+x);
    oos.writeObject(x);
    oos.flush();
    ois.close();
    oos.close();
    client.close();
    } catch(Exception e) {}

    I print out what is received at server:
    java.awt.Event[id=1001,x=30,y=1,target=java.awt.Button[button2,30,1,60x40,label=Write],arg=Write]
    Is that mean server has already received the event?
     
    juicy, Sep 24, 2004
    #17
  18. On Thu, 23 Sep 2004 22:18:41 -0400, juicy wrote:
    > Yes, i have imported java.awt.Event but the result is still same.
    > Is that really the server cannot read the event?


    Which process - client or server - gets the ClassNotFoundException
    from readObject()? You weren't clear about that.

    Do both processes use the same version of the JRE (and class
    libraries)?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 24, 2004
    #18
  19. On Fri, 24 Sep 2004 00:48:18 +0200, Michael Borgwardt wrote:

    > Andrew Thompson wrote:

    ...
    >> Your server does not need it till run time,
    >> that is why your code compiles, but to recognize
    >> an Event you are reading, the java.awt.Event
    >> must be imported to the server code.

    >
    > That can't be right.


    No. I started on a wrong concept then went
    happily with it. Thanks for correcting me.

    And, to the OP. Apologies for the 'noise'.

    But.. I strongly suggest you make a minimized test
    case of code (you can do probably do this in under
    100 lines) that displays the problem.

    Post it on the group.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.lensescapes.com/ Images that escape the mundane
     
    Andrew Thompson, Sep 24, 2004
    #19
  20. juicy

    juicy Guest

    Sorry, is at client side gets the ClassNotFoundException
    from readObject().
     
    juicy, Sep 24, 2004
    #20
    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. Serial # 19781010
    Replies:
    1
    Views:
    736
    Roedy Green
    Jul 15, 2003
  2. mitch gart
    Replies:
    1
    Views:
    379
    David Zimmerman
    Oct 18, 2003
  3. Jim Garrison
    Replies:
    0
    Views:
    423
    Jim Garrison
    Oct 21, 2003
  4. Replies:
    1
    Views:
    4,446
    Harish
    Jan 7, 2005
  5. _kOws
    Replies:
    1
    Views:
    2,666
    _kOws
    Aug 20, 2007
Loading...

Share This Page