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(ServS ocket.getO utputStrea m());
//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(clientSo cket);
********The thread class PrivateConnection*******
Socket clientSocket=null;
PrivateConnection(Socket s)
{
clientSocket = s;
start();
}
public void run()
{
BufferedInputStream in = new BufferedInputStream(client Socket.get InputStrea m());
//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_mes sage)ois.r eadObject( );
//when this second object is deserialized it still contains the old information, message.message is still ='s'.
message=(Client_server_mes sage)ois.r eadObject( );
}//run
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(ServS
//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(clientSo
********The thread class PrivateConnection*******
Socket clientSocket=null;
PrivateConnection(Socket s)
{
clientSocket = s;
start();
}
public void run()
{
BufferedInputStream in = new BufferedInputStream(client
//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_mes
//when this second object is deserialized it still contains the old information, message.message is still ='s'.
message=(Client_server_mes
}//run
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
Computer101
E-E Admin
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();
========================== ========== ========== ==========
==========================
message = null;
message = new Client_server_message();
message.message='u';
oos.writeObject(message);
oos.flush();
==========================
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.