Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ObjectStream problem *urgent*

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I have a problem handling ObjectOutputStream/ObjectInputStream.

I created a Thread-Class that writes different objects on an ObjectOutputStream (getting the objects from a Vector). And there is a Thread that reads from the ObjectInputStream. However... if I send an object of let's say type "A", the object will be read by the ObjectInputStream-thread in the right way. But if I send the object of type "A" again (with different properties/values this time) the ObjectInputStream-thread will read the object with the old properties/values. Why??

Here are the two classes:

Code:
import java.io.*;

public class StreamReader extends Thread
{
    	Object obj;
    	VectorHandler vh;
    	ObjectInputStream in;
    	ExitHandler exit;
    	
    	private static final int NORMALEXIT	= 0;
    	private static final int STREAM 	= 2;
	private static final int LOGINCANCEL	= 3;    	
    	private static final int OBJECTERROR	= 4;
    	
    	
    	public StreamReader(ObjectInputStream in, VectorHandler vh, ExitHandler exit){
    		this.vh = vh;
    		this.in = in;
    		this.exit = exit;
    	}
    	
    	
	public void run()
	{
		// keep thread alive until exit-signal is caught
		while( !exit.getStatus() )
		{
		 try
		 {
			obj = in.readObject();
			if( obj instanceof ExitObj ){	// check for exit-object
			 exit.setStatus(true);		// set exit-signal
			 exit.setReason( ((ExitObj)obj).getReason() );
			 System.out.println("DEBUG-INFO: exit-object was read by StreamReader!");
			}
			else
			 vh.putElement(obj);		// put object in the vector
			System.out.println("DEBUG-INFO: object was read by StreamReader!");
		 }
		 catch(IOException e){
			if( !exit.getStatus() && exit.getReason() != LOGINCANCEL )
			{
                        System.out.println("FATAL ERROR: IOException caught -> Could not write object-stream!\n" +
                                           "REASON:      Either a network failure occurred or client wasn't closed as it should.\n" +
                                           "STATUS:      Client disconnected. Trying to close socket...");
			 //e.printStackTrace();
			 exit.setStatus(true);
			 exit.setReason(STREAM);
			}
		 }
		 catch(ClassNotFoundException cnfe){
			System.out.println("FATAL ERROR: ClassNotFoundException caught, try to close socket...");
			exit.setStatus(true);
			exit.setReason(OBJECTERROR);
		 }

		}
		System.out.println("DEBUG-INFO: exiting thread StreamReader....");
	}
}




import java.io.*;

public class StreamWriter extends Thread
{
	private static final int STREAM = 2;
	
	VectorHandler vh;
	ObjectOutputStream out;
	ExitHandler exit;
	Object obj;
    	
	public StreamWriter(ObjectOutputStream out, VectorHandler vh, ExitHandler exit)
	{
		this.vh = vh;
		this.out = out;
		this.exit = exit;
	}
    	
	public void run()
	{
		// keep thread alive until exit-signal is caught
		while( !exit.getStatus() )
		{
		 try
		 {
		  // read first element of vector and put it on the stream
	 	  obj = vh.getElement();
	 	  //out.writeObject(vh.getElement());
	 	  out.writeObject(obj);
		  out.flush();
		  System.out.println("DEBUG-INFO: object was written by StreamWriter!");
		 }
                 catch(IOException e){
                        System.out.println("FATAL ERROR: IOException caught -> Could not write object-stream!\n" +
                                           "REASON:      Either a network failure occurred or client wasn't closed as it should.\n" +
                                           "STATUS:      Client disconnected. Trying to close socket...");
                        //e.printStackTrace();
                        exit.setStatus(true);
                        exit.setReason(STREAM);
                 }
		}
		System.out.println("DEBUG-INFO: exiting thread StreamWriter....");
	}
}


cheers

frag

patrick.metz@epost.de
 
it means
1. you send object berofe updating
2. you send the old object
3. you set the new object to old object somewhere


Ion Filipski
1c.bmp
 
1. Definitely not!
2. Yes, but with different values ;-)
3. This is what I was thinking of too, but... the object has the right values before it gets written to the stream and if I check the object's values right after reading it from the stream I get the old values again. There is no possibility to accedently have a reference to the old object I think.

patrick.metz@epost.de
 
I don't see in your code the place where you update the object before sending again.

Ion Filipski
1c.bmp
 
You can't see it, because it's not getting updated there directly. Look at that line:
Code:
obj = vh.getElement();

This will return the first element/object in a Vector and deletes the object from the Vector (pop). After that "obj" is written to the stream. Which works fine for the first object but not for the second object.

It is hard to explain that. We are talking about a client-server application... look... I have a login-dialog (with account information like name, pwd, etc.) on the client-side. If the login button is pressed a thread is started which will write the login object (with the data from the the dialog) into a vector. The class StreamReader is polling on this vector for a new entry. If there is a new entry it will get collected by StreamReader and will be written on the stream. On the server-side the object gets read and processed an will be returned by writing it on the stream again. On the client-side: The same thread that has written the object now waits for the object's return. It will be read and if the login was not successfull the user can change his login details and try again... and this "try again" won't work because there will be the old values from his first try in the object that will be send to the server...

patrick.metz@epost.de
 
sorry the old values are not on the client side but on the server side (after reading the object for the second time)...

patrick.metz@epost.de
 
after you obj = vh.getElement(); look what is inside ur obj. What you put is what you get, and java does not have bugs in reading and writing objects.

Ion Filipski
1c.bmp
 
Ok... solved it on my own. Too hard to explain... it was a problem with the object-reference on the client side... at least I guess it was. I now create a temp. object (with new) before sending and copy the values from the original object into the temp. object which gets written onto the stream (instead of the original object)... just forget it... it works and that's what counts ;-)

patrick.metz@epost.de
 
It is not a matter of sending the "old" or the "new" object. There is only ONE object.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top