Socket programming in Java

j3kitten
j3kitten used Ask the Experts™
on
Hello.
I have a problem with client-server programming. I put the information I want to send to the server in a object. I serialize the object and sends it, and on the server side I deserialize it and reads the information. This works the first time I try it, but when I want to send an updated object to the same stream something goes wrong. The server still has the old object after deserialization.

I would be really greatful if someone could give me some advise how to solve this problem.
Thanks

Tobias
*****client side code**********
BufferedOutputStream out=null;  
Socket ServSocket = new Socket('localhost',4444);
out=new BufferedOutputStream(ServSocket.getOutputStream());

//this is the object I want to send to the server    
Client_server_message message=new Client_server_message();
ObjectOutputStream oos=new ObjectOutputStream(out);
message.message='s';  
oos.writeObject(message);
oos.flush();

//and then I update the object and send it again  
message.message='u';
oos.writeObject(message);
oos.flush();
*********server side code**************    
ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
//open a new thread for one user
new PrivateConnection(clientSocket);

********The thread class PrivateConnection*******  
Socket clientSocket=null;
PrivateConnection(Socket s)
 {
  clientSocket = s;
  start();
 }      
  public void run()
 {      
 BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
     
//deserialize
Client_server_message message=new Client_server_message();
ObjectInputStream ois=new ObjectInputStream(in);

//here comes the first object and it works fine
message=(Client_server_message)ois.readObject();

//when this second object is deserialized it still contains the old information, message.message is still ='s'.
message=(Client_server_message)ois.readObject();
}//run          
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Better post your reading code. Are you committed to retaining the original message? If not, you could set it to null between reads.
A request for deletion has been made.  If no objections, I will do this in 72 hours

Computer101
E-E Admin
Mick BarryJava Developer
Top Expert 2010

Commented:
I won't post an answer then :)
Before it send data again, set the message object is null and then new the message object again.
========================================================
message = null;
message = new Client_server_message();
message.message='u';
oos.writeObject(message);
oos.flush();
========================================================
Commented:
PAQ'd and points refunded

As the additional comments are posted after the delete request comment.

modulo

Community Support Moderator
Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial