This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Java Sockets receiving the same array packet over and over again  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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.

Thanks for help a lot!



 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to debug the code?  I don't see any print statements to show what is being sent.

How can the code be compiled and executed for testing?  Do you have a small, simple program that compiles, executes and shows the problem?
 
Jebiga StaMozed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I still don't understand why this happens.


Very hard to say what the problem is without seeing code that executes and shows the problem.
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Henry

 
 
Jebiga StaMozed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!