DatagramSocketChannel returning same data over and over

M

Michael Ansel

I have written a method to read the next incoming packet from a
non-blocking DatagramSocketChannel, convert it into a DatagramPacket and
return it. The method works perfectly the first time it is called, but
as soon as the method is called with NEW data waiting on the channel,
the data from the first message is returned. For example:

--Loop--
receiveFromSocket()
No data waiting, return null
--End Loop--
receiveFromSocket()
Data waiting from node1, return msg1
--Loop--
receiveFromSocket()
No data waiting, return null
--End Loop--
receiveFromSocket()
Data waiting from node2, return msg2
--Loop--
receiveFromSocket()
No data waiting, return null
--End Loop--
receiveFromSocket()
New data waiting from node1, still returns msg1, should return msg3

This happens every time, and I cannot figure out why the
DatagramSocketChannel is always returning the same data when a new
message is received from the same address. At the end of this message is
the method code. The entire source code is online at
http://code.google.com/p/waefers-bum/source/ . This method is inside
net.waefers.Messaging.MessageControl .

There is also a log file inside the default package. The software sends
a message from ReplicaMaster to NodeMaster successfully, and
ReplicaMaster receives the reply from NodeMaster successfully. However,
when TestPeer connects to NodeMaster successfully, and causes NodeMaster
to send a message to ReplicaMaster, ReplicaMaster receives the heartbeat
response packet again instead of the new data.

If you are intending to run the software and test it, first run
NodeMaster, then run ReplicaMaster. RM will heartbeat with NM. Finally,
run TestPeer. TP will attempt to heartbeat with NM. NM will attempt to
update block on RM, but RM will not receive the message and TP and NM
will continuously attempt to resend the (apparently) lost messages.

I hope the code is commented well enough easy understanding. If not,
please feel free to ask questions. I am working on this for a high
school independent study project, and I would really like to fix this
little bump in the road so I can move on with the software. I greatly
appreciate your time!

Sincerely,
Michael Ansel
(e-mail address removed)

private static DatagramPacket receiveFromSocket() throws IOException {
synchronized(rbuf) {
/* Clear the receive buffer before doing anything */
rbuf.clear();
/* Get the next packet */
SocketAddress addr = server.receive(rbuf);
if(addr == null) return null;
/* Get the buffer ready for reading */
rbuf.flip();
/* Turn the data into a packet */
DatagramPacket pkt = new DatagramPacket(rbuf.array(),rbuf.limit());
pkt.setSocketAddress(addr);

return pkt;
}
}
 
E

EJP

Err,

Not Found
The requested URL /p/waefers-bum/source/ was not found on this server.

but new DatagramPacket(byte[], int) doesn't copy the byte[], just the
array reference, so you are always going to be referring to the same
memory location. This would mean that old packets would get overwritten
by new datagrams, not vice versa, but if you are comparing byte[]
references rather than their contents this would be consistent with with
your symptoms.
 
M

Michael Ansel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sorry about the link. No trailing slash. So, it should be
http://code.google.com/p/waefers-bum/source

I'm not sure I understand what you are saying. Whenever I run the
method, it is returning the exact same packet(content-wise). This should
have nothing to do with references, etc. Thanks for your help though!

Michael

Err,

Not Found
The requested URL /p/waefers-bum/source/ was not found on this server.

but new DatagramPacket(byte[], int) doesn't copy the byte[], just the
array reference, so you are always going to be referring to the same
memory location. This would mean that old packets would get overwritten
by new datagrams, not vice versa, but if you are comparing byte[]
references rather than their contents this would be consistent with with
your symptoms.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFST5Sku86QLyIARQRAqYQAKCmdhTyZPObW1auZwNf9Ym6PM57IACdFF2p
IArRlCOecCpxQfRhA5ihkCc=
=4z7N
-----END PGP SIGNATURE-----
 
E

EJP

Michael said:
I'm not sure I understand what you are saying. Whenever I run the
method, it is returning the exact same packet(content-wise). This should
have nothing to do with references, etc. Thanks for your help though!

You have:

1. private static ByteBuffer rbuf = ByteBuffer.wrap(new byte[1472]);

2. DatagramPacket pkt = new DatagramPacket(rbuf.array(),rbuf.limit());

3. rbuf never changes.

4. rbuf.array() never changes its location in memory.

5. new DatagramPacket(rbuf.array(), ....) doesn't copy rbuf.array(), it
just uses it as is where is.

So,

6. The next read into rbuf will overwrite the data for every previously
DatagramPacket in existence. So they will always all appear to have the
same data. Because they do.
 
M

Michael Ansel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm probably still misunderstanding this, but wouldn't that mean that
the old data would be over written by the new? The problem I am having
is that the new data isn't showing up. Also, do you have any idea how I
could fix this? Thank you for all your help!

Michael
Michael said:
I'm not sure I understand what you are saying. Whenever I run the
method, it is returning the exact same packet(content-wise). This should
have nothing to do with references, etc. Thanks for your help though!

You have:

1. private static ByteBuffer rbuf = ByteBuffer.wrap(new byte[1472]);

2. DatagramPacket pkt = new DatagramPacket(rbuf.array(),rbuf.limit());

3. rbuf never changes.

4. rbuf.array() never changes its location in memory.

5. new DatagramPacket(rbuf.array(), ....) doesn't copy rbuf.array(), it
just uses it as is where is.

So,

6. The next read into rbuf will overwrite the data for every previously
DatagramPacket in existence. So they will always all appear to have the
same data. Because they do.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFSq8pku86QLyIARQRAhHtAKDNGye022dYMnq4AAnac1hP3w4RwwCaAhQ6
ZcTuCXiPamZ/MaQpds7TO3M=
=3pId
-----END PGP SIGNATURE-----
 
E

EJP

Michael said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm probably still misunderstanding this, but wouldn't that mean that
the old data would be over written by the new? The problem I am having
is that the new data isn't showing up.

Are you sure?

In any case you must copy the data out of the ByteBuffer into a new byte
array when creating the DatagramPacket.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,099
Members
48,773
Latest member
Kaybee

Latest Threads

Top