writeObject and readObject problem

J

juicy

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?
 
G

Gordon Beaton

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
 
N

newton klea

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.
 
J

juicy

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
 
J

juicy

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?
 
G

Gordon Beaton

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
 
J

juicy

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?
 
A

Andrew Thompson

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
 
J

juicy

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

Andrew Thompson

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
 
J

juicy

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?
 
G

Gordon Beaton

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?

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
 
J

juicy

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??
 
A

Andrew Thompson

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
 
M

Michael Borgwardt

Andrew said:
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.
 
J

juicy

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?
 
G

Gordon Beaton

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
 
A

Andrew Thompson

Andrew Thompson wrote: ...

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top