writeObject and readObject problem

J

juicy

//Object.html
<html>
<head>
<title>VE</title>
</head>
<BODY>
<DIV align="left">&nbsp; &nbsp;</DIV>
<APPLET id="Mainvirtual_dispatch1" height="176" width="568"
code="Client.class"
VIEWASTEXT mayscript></APPLET>
<DIV></DIV>
<DIV align="left"></APPLET>
</DIV>
</BODY>
</html>


//Server.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Event;

public class Server extends Thread {

public ServerSocket arrayServer;
public static void main(String argv[]) throws Exception {
new Server();
}

public Server() throws Exception {
arrayServer = new ServerSocket(1053);
System.out.println("Server listening on port 1053.");
this.start();
}

public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = arrayServer.accept();
System.out.println("Accepted a connection from: "+
client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}

class Connect extends Thread {
public Socket client = null;
public ObjectInputStream ois = null;
public 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 = new Object();
try {
x = ois.readObject();
if(x!=null)
System.out.println("object has been read");
oos.writeObject(x);
oos.flush();
ois.close();
oos.close();
client.close();
} catch(Exception e) {e.getMessage();}
}
}

//Client.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Client extends Applet implements Runnable{

Socket client;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Button Connect,write,read;
Thread recvThread;
String host;
boolean running=true;

public void init()
{

host = getCodeBase().getHost();
add(Connect = new java.awt.Button());
Connect.setLabel("Connect");
Connect.setBackground(new Color(0xd3ceac));
Connect.setForeground(new Color(0x000000));
Connect.show(true);

add(write = new java.awt.Button());
write.setLabel("Write");
write.setBackground(new Color(0xd3ceac));
write.show(true);

add(read = new java.awt.Button());
read.setLabel("Read");
read.setBackground(new Color(0xd3ceac));
read.show(true);
}

public void start(){
if(recvThread==null){
recvThread = new Thread(this);
recvThread.start();}
}

public void destroy(){
running = false;
recvThread = null;
}

public boolean action(Event event, Object what) {
if (recvThread == null) {
start();
}

if (event.target instanceof Button){

Button b = (Button) event.target;

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


if (event.arg == "Write") {

try{
oos.writeObject((Object)event);
oos.flush();
}
catch(Exception e){ }
}

if (event.arg == "Read") {

try{
Object get=null;
get=ois.readObject();
if(get!=null)
Connect.setLabel("success");
else
write.setLabel("error");
oos.close();
ois.close();
}

catch (IOException e) {Connect.setLabel("ex1");}
catch (ClassNotFoundException e){Connect.setLabel("ex2");}
catch (Exception e){Connect.setLabel("ex3");}
}
}
return true;
}
public void run(){}
}

The problem is:
the client cannot read the object send back from server and
ClassNotFoundException occur.
 
G

Gordon Beaton

The problem is:
the client cannot read the object send back from server and
ClassNotFoundException occur.

I'll ask this again: do the client and server run on the *same*
version of the JRE?

I suspect that a client running an old version of Java can send Event
objects to a server running a newer version, and that the server will
be able to read the objects even though it might see a newer version
of the class than the one sent by the client.

The converse is most likely not true; if a server running a new
version of Java sends Event objects to a client running an older
version, the client won't recognize the newer version of the class.
Exactly what exception that should cause I don't know (and haven't
bothered to test).

Here are some things to try:

- try running your client as an application (not an applet) and run it
and the server using the same JRE version. Does it work?

- next, define an event class of your own that extends only Object
and contains as little as possible (maybe nothing). Again test your
applet with the server. Does it still work?

- finally, avoid creating an instance of the Event object at the
server unless you need to. Let the server transfer the raw bytes
from its SocketInputStream to the SocketOutputStream as I suggested
in an earlier post.

/gordon
 
M

Michael Borgwardt

I've run the posted code with no problems using JRE 1.4.2 - the client shows "success".
Couldn't try it with the Microsoft VM as client since it won't allow net access to
127.0.0.1 when showing a local file...

Gordon said:
I suspect that a client running an old version of Java can send Event
objects to a server running a newer version, and that the server will
be able to read the objects even though it might see a newer version
of the class than the one sent by the client.

The converse is most likely not true; if a server running a new
version of Java sends Event objects to a client running an older
version, the client won't recognize the newer version of the class.

I'm almost certain that is exactly what's happening with the Microsoft VM here.
Exactly what exception that should cause I don't know (and haven't
bothered to test).

It *should* throw a java.io.InvalidClassException, but the Microsoft VM is
broken in many ways, especially in that regard. It also throws a
ClassNotFoundException instead of an UnsupportedClassVersionError when
encountering a class file with a version it doesn't support.
 
J

juicy

What should i do? I cannot change to use JRE because i need Microsoft VM to
run my virtual reality part.
:( Can anybody help??!!
 
A

Andrew Thompson

I use Microsoft VM to run the client and server.

Your code suggests to me that you are still
not getting the best error information.

Every time your code has ..

} catch (Exception e) {
...

...the very next thing should be..
e.printStackTrace();

The server exceptions will appear in the command line,
the client in the MS Java concole at
Alt V(iew) | J(ava Console)

I am still looking at your code.

Your exception handling could be simplified by putting
everything in the method within a single try/catch.

You are using deprecated and unnecessary (even for
the MSVM) methods. Use the -XLint method option
of javac for further details. (In case you are
*compiling* with MS SDK - stop it and use Sun
Java with options)

method 'action()' is also deprecated.

If you are interested in ActionEvents, as you
seem to be - with the button clicks, why not
implement ActionListener and send an ActionEvent?

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

Andrew said:
Your exception handling could be simplified by putting
everything in the method within a single try/catch.
ok. I am still a beginner in java...
(In case you are
*compiling* with MS SDK - stop it and use Sun
Java with options)

I must use MSVM to run my another virtual reality part which is controlled

by java applet, if not, i will get all the exception like below.

java.lang.UnsatisfiedLinkError: getBrowserType
at vrml.cosmo.Browser.getBrowserType(Native Method)
at vrml.external.Browser.getBrowser(Browser.java)
at vrml.external.Browser.getBrowser(Browser.java)
at VE.init(VE.java:118)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

java.lang.UnsatisfiedLinkError: getBrowserType
at vrml.cosmo.Browser.getBrowserType(Native Method)
at vrml.external.Browser.getBrowser(Browser.java)
at vrml.external.Browser.getBrowser(Browser.java)
at VE.init(VE.java:118)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

java.lang.UnsatisfiedLinkError: getBrowserType
at vrml.cosmo.Browser.getBrowserType(Native Method)
at vrml.external.Browser.getBrowser(Browser.java)
at vrml.external.Browser.getBrowser(Browser.java)
at VE.init(VE.java:118)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

java.lang.UnsatisfiedLinkError: getBrowserType
at vrml.cosmo.Browser.getBrowserType(Native Method)
at vrml.external.Browser.getBrowser(Browser.java)
at vrml.external.Browser.getBrowser(Browser.java)
at VE.init(VE.java:118)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
method 'action()' is also deprecated.
Why method ation is deprecated?
If you are interested in ActionEvents, as you seem to be ->with the
button clicks, why not implement ActionListener >and send an ActionEvent?
If i implement ActionListener and send an ActionEvent, the result is still
same if i run the code with MSVM, right?

I don't know who is still willing offer help... what should i do now?? I
will really appreaciate your help!!
 
A

Andrew Thompson

I must use MSVM to run my another virtual reality part which is controlled
by java applet, if not, i will get all the exception like below.

java.lang.UnsatisfiedLinkError: getBrowserType
at vrml.cosmo.Browser.getBrowserType(Native Method)

This thread* suggests such code will work only in the MSVM,
is that correct? Is this Applet MSVM *only*?!?
<http://www.talkaboutprogramming.com/group/comp.lang.vrml/messages/32634.html>

You are coding for a dangerous dinosaur. That particular VM is
one that I make software specifically to help people *remove*.
Why method ation is deprecated?

Because time goes on. That is irrelevant (you not
need change) if you are coding only for MSVM.
I don't know who is still willing offer help... what should i do now?? I
will really appreaciate your help!!

My interest in helping you write code has plummeted since
I realised it was for a broken and insecure VM only.
I have far more ..this millenium things to be doing.

My advice is. Give up on this folly.

MS itself no longer provides IE with the MSVM, and at the
rate they are being *uninstalled*, unless you get your
project finished and make your millions off it within 6
months, you will end up with a piece of software for which
no-one has the plug-in. It will be useless.

--
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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top