Reputation: 273
I have a question on TCP client / server network. Im trying to create a chat application. My plan is for the client to send Conversation objects across the network (object has a String username state and a String currentMessage state). My question is, should i be creating a new object each time i send a message? i.e call the Conversation class constructor each time i want to send a new message? or should i be just updating the currentMessage state?? I have this question because at the moment, i have it just updating the state and here is what happens:
The currentMessage state updates fine on the client side, but when i send the Conversation object across the network and try to retrieve the currentMessage state on the other side i keep getting the original state value. Why is this? The server keeps refering to the original message value i sent the first time??
PS I dont want to be posting reams of code so im hoping someone might have a general idea what im doing wrong
Upvotes: 1
Views: 342
Reputation: 307
Are you using the ObjectOutputStream class to serialize your Conversation object? If so, it will cache classes that you've already written to the stream, so even when you change the values client-side, every time you write that object, it will send a pointer to that object, saving a great deal of time and bandwidth. Unfortunately, that's not always your goal, so you're left with have two choices:
Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.
You can read a great article about this here.
I personally find it cleaner to create new objects rather than be resetting the stream before (or after) every write, however if you're in a situation where the allocation of your object is rather large and you're only changing a couple fields between network writes, it may be better to reset the stream.
This kind of optimization, however, is probably best put off until later in your project when you can do some real profiling to see which works better for you. My advice is to pick whichever you're most comfortable with and move forward until you're at a point that optimization matters. Always remember, premature optimization is the bane of good programming! : )
Upvotes: 1
Reputation: 5681
You can use the flyweight design pattern.
Basically you can have an object that is always the same. You wrap it in another object. In your case the object you send will be the same every time. The only thing you change is the text.
While I'm not sure how your code works, the way I see it is, (1) create a new object to send every time or (2) create one and keep setting different text to send in it (which is kinda like flyweight).
The former way, you can preallocate a large list of these envelopes, and consume them one by one as you send the message. This will improve performance.
As for your other question, I agree w Matt and Hasslam. What you're sending is probably cached so you need to refresh it, or rather let Java know it has been changed.
Upvotes: 0
Reputation: 3929
Im guessing you are using an ObjectOutputStream or something similar?
Had this problem my self some time ago. When you send the first time it works fine, but after that it doesnt update properly.
This is because the stream saves the object state, and if it is sent again it doesnt update. You need to use the reset() method after you received an object to make it update again.
Upvotes: 1