I need to pass an int array from the server to the client many times.
The first time that I send the array, everything works perfectly, but when I send it the second time or third etc. it arrives to the client as the exact same array that I've sent the first time.
If I send first time: 1, 0, 0, 0
The client will receive: 1, 0, 0, 0
And no matter what I send afterwards, my client will always continue receiving 1, 0, 0, 0 forever.
You might think that there is a problem with my sockets, streams etc. but I doubt it because I tried sending other data that isn't arrays and it worked perfectly.
The way I fixed my problem was by turning the int to Integer. Now it all works, but I just don't understand how that is possible. Can somebody explain?
Here is the code of my connection class which is responsible for accepting packets and sending them.
By the way, I also checked if what I send is changing and if what I receive is really the same and it really is. As I said just turning the array from int to Integer fixed the problem, but I can't get why.
I didn't put any print statements here to show you, but, actually I'm making a multiplayer TicTacToe. The thing is, every time the game state changes, the server sends the two dimensional array of integers to tell the client the current game state. When I send the first update, it updates perfectly, but when I send the second packet. It just looks the same as the previous packet. BTW. I did debug it with printing out and it sends something different, but receives the old thing. I found a solution to this. Basically, before sending I serialize the array and then deserialize it by myself. It started working again, but I still don't understand why this happens.
Norm Radder wrote:
Very hard to say what the problem is without seeing code that executes and shows the problem.
I can take a guess.... There is an optimization with serialization. When you send the same object again, the system will detect it, and mark it as a resend. This means that you can't send a mutable object, make some changes, and send the same object again. The other side will detect it as the same object, and return the same reference.
If you want the Object stream to disregard previous sends, then use the reset() method.
Thank you so much Henry, that is exactly what was happening. That is exactly why serializing it by myself before sending helped it, because it created a new object every time because I was serializing it by myself. I deleted my serialization and I added outputStream.reset(); and now it works perfectly!