Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

(ObjectInputStream) first time it works, second time I get an error!

843790Apr 14 2007 — edited Apr 17 2007
hi,

Using the following code I am sending a query to a server socket using a PrintWriter.println function and retrieve a vector object (results).

Client Code
	public Vector getDBResultSet(String query) throws ClientException, IOException{
		Object obj = new Object();
		if (socket != null)
		{
			try{				
				System.out.println("Sending "+query+" to Server");
				writer.println(query);
				try{
					ois = new ObjectInputStream(input);
					while ((obj = ois.readObject()) != null)
						obj = ois.readObject();
				}catch (EOFException eofx){
					System.out.println("EOF @ object input stream!");
				}
				finally{
					ois.close();
				}
				return (Vector)obj;
			}catch(Exception e){
				e.printStackTrace();
				throw new ClientException("<html>Error: <br>"+e.toString()+"#"+e.getMessage()+"</html>");
			}finally{
				//writer.close();
			}
		}else throw new ClientException("no connection!");
	}
Server side code
	public void communicate()
	{
		try{		
			while (true){
				String q = reader.readLine();
				Vector vec = db.getResults(q);
				oos = new ObjectOutputStream(output);
				oos.writeObject(vec);
				oos.flush();
				oos.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
First time everything goes OK.

Second time on the Client-Side I get strange errors like "ClassCastException Java.lang.Object" and if I comment out the "while" line I get a "socket closed" error...

any ideas what is wrong with it?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2007
Added on Apr 14 2007
7 comments
347 views