DatagramSocketChannel returning same data over and over

Discussion in 'Java' started by Michael Ansel, Oct 31, 2006.

  1. 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


    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;
    }
    }
    Michael Ansel, Oct 31, 2006
    #1
    1. Advertising

  2. Michael Ansel

    EJP Guest

    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.
    EJP, Nov 1, 2006
    #2
    1. Advertising

  3. -----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


    EJP wrote:
    > 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-----
    Michael Ansel, Nov 2, 2006
    #3
  4. Michael Ansel

    EJP Guest

    Michael Ansel wrote:

    > 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.
    EJP, Nov 2, 2006
    #4
  5. -----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

    EJP wrote:
    > Michael Ansel wrote:
    >
    >> 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-----
    Michael Ansel, Nov 3, 2006
    #5
  6. Michael Ansel

    EJP Guest

    Michael Ansel wrote:
    > -----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.
    EJP, Nov 4, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cesar Zam
    Replies:
    0
    Views:
    337
    Cesar Zam
    Nov 6, 2003
  2. Replies:
    11
    Views:
    661
    Christos Georgiou
    May 2, 2006
  3. Christopher Brewster
    Replies:
    5
    Views:
    339
    John Machin
    Nov 14, 2008
  4. John Smith
    Replies:
    1
    Views:
    112
    William Herrera
    Nov 29, 2003
  5. Replies:
    5
    Views:
    140
Loading...

Share This Page