*
The moose likes Java in General and the fly likes Can I sit on ois.readObject() and wait for more packets? More questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can I sit on ois.readObject() and wait for more packets? More questions" Watch "Can I sit on ois.readObject() and wait for more packets? More questions" New topic
Author

Can I sit on ois.readObject() and wait for more packets? More questions

joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
I'm writing a server/client (for learning purposes) which will allow the client to talk to other clients (in the future do other stuff.) I have ran into a wall figuring why my program is not working the way I think it should. I have some questions and below supplied code if needed..

1. Can I sit on readObject() waiting for my client to send next packet, and if not is there a way to wait for a packet to be sent?

2. I created a Packet object that contains a int and a String that I will (or want to) use for my communications. Can that be sent without special handling (as is(Packet class is below))

Now the code works perfect first time through sends receives packet as planned. However when i send a new packet. nothing it is zero'd out.
I think my answer will be in the above two questions but if not WHY? <SMILE.

Any help would be appreciated.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

joe vasher wrote:1. Can I sit on readObject() waiting for my client to send next packet, and if not is there a way to wait for a packet to be sent?

You don't have any choice. The readObject() method waits until there is an object in the input stream for it to receive.

2. I created a Packet object that contains a int and a String that I will (or want to) use for my communications. Can that be sent without special handling (as is(Packet class is below))

Yep. Packet implements Serializable, that's all you need.

Now the code works perfect first time through sends receives packet as planned. However when i send a new packet. nothing it is zero'd out.

There's a lot of code there but as far as I can tell you aren't sending a new packet the second time. You're sending the same packet as you did the first time. So your ObjectOutputStream will recognize that fact and not bother to send the data. Instead it just sends a notification saying that it's the same packet as the first one, and the ObjectInputStream at the other end uses the first packet again.

What's the point in clearing a packet? Just create a new one when you need another one.
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
I kinda see what your saying, because when I print what is in the packet when it gets to the server the 2ND** time it is the same as the first time. However When I print out pResult (which is the updated packet from the GUI) it has the new values)

"Just when I thought I had it...."
OUTPUT FROM CLIENT...
Client Window Open
switch 4
notify thread
donewaiting 4
WNC C: 4 TXT: test
before write C: 4 TXT: test <<<<< pResult BEFORE WRITE OBJ first time
CL C: 4 TXT: test
switch 4
notify thread
donewaiting 4
WNC C: 4 TXT: another test
before write C: 4 TXT: another test <<< THIS IS pResult before it goes to writeObject(); 2nd time
CL C: 0 TXT:

OUTPUT FROM SERVER....
Waiting for connections. 15
C C: 4 TXT: test <<<<< FIRST WRITE OBJECT READ FROM SERVER
command value: 4 for ID: 18
C C: 4 TXT: test <<<<< SECOND WRITE OBJECT
command value: 4 for ID: 18


joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Well I got it working, so that It will accept two packets and send them around the loop. However, it feels like a hack. Would really appreciate it if someone would check and see if I have done something wrong with my above code.

The fix was a simple oos.reset() after the write object in client.

thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Yeah, doing a "reset" on the ObjectOutputStream is a bit of a hack. It does allow you to resend the contents of an object a second time, after you change them. But to me a "packet" doesn't sound like something you should be changing the contents of. You should be creating new packets for new data.
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Will the creation of the Packet below cause a memory leak? The reason I ask, is when I run the client and type a message and send. Memory increases by about 200 bytes everytime (per taskmanager.)

Thanks in advance.

The constructor that i'm using for packet is..

Packet(Packet p ) {
this.command = p.command;
this.text = p.getText();
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I sit on ois.readObject() and wait for more packets? More questions