Link to home
Start Free TrialLog in
Avatar of j3kitten
j3kitten

asked on

Socket programming in Java

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          
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
I won't post an answer then :)
Avatar of Lavendermpl
Lavendermpl

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();
========================================================
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial