Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

DatagramSocketChannel returning same data over and over

Thread view: 
Michael Ansel - 31 Oct 2006 14:52 GMT
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
news@anselcomputers.com

    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;
        }
    }
EJP - 01 Nov 2006 09:24 GMT
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.
Michael Ansel - 02 Nov 2006 01:39 GMT
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,
>
[quoted text clipped - 7 lines]
> references rather than their contents this would be consistent with with
> your symptoms.
EJP - 02 Nov 2006 07:19 GMT
> 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.
Michael Ansel - 03 Nov 2006 03:53 GMT
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

>> 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
[quoted text clipped - 18 lines]
> DatagramPacket in existence. So they will always all appear to have the
> same data. Because they do.
EJP - 04 Nov 2006 03:36 GMT
> -----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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.