• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Java Sockets receiving the same array packet over and over again

 
Jebiga StaMozed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Norm Radder
Rancher
Posts: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Angel Chavez
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was having the same issue, thanks so much, I solved it because of you both!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic