Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > writeObject and readObject problem

Reply
Thread Tools

writeObject and readObject problem

 
 
juicy
Guest
Posts: n/a
 
      09-21-2004
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?

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-21-2004
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
 
Reply With Quote
 
 
 
 
newton klea
Guest
Posts: n/a
 
      09-21-2004
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.
 
Reply With Quote
 
juicy
Guest
Posts: n/a
 
      09-22-2004
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

 
Reply With Quote
 
juicy
Guest
Posts: n/a
 
      09-22-2004
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?

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-22-2004
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
 
Reply With Quote
 
juicy
Guest
Posts: n/a
 
      09-22-2004
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?




 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-22-2004
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
 
Reply With Quote
 
juicy
Guest
Posts: n/a
 
      09-22-2004
I am using Microsoft VM instead of JVM, is the exception because of no
certain class in Microsoft VM?

 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-22-2004
juicy wrote:

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


Probably not, but again:

http://www.physci.org/codes/javafaq.jsp#exact
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with readObject and EOF Exception _kOws Java 1 08-20-2007 02:53 PM
Tuning the performance of ObjectInputStream.readObject() kannan123@gmail.com Java 1 01-07-2005 07:01 PM
Serialization: readObject() and self method calls? Jim Garrison Java 0 10-21-2003 06:01 PM
question on serialization and readObject() mitch gart Java 1 10-17-2003 11:34 PM
objectInputStream.readObject() does not work properly after certain period of inactivity. Serial # 19781010 Java 1 07-15-2003 10:20 PM



Advertisments