title: When does a DatagramPacket have its IP field changed from destinationIP to sourceIP?
body: <p>I am trying to get familiar with the Datagram system in java for a project, currently, we are only working with UDP packages.</p>
  
  <p>To send a message we set the destination ip on the DatagramPacket.</p>
  
  <pre><code>    /*
       * The fields of this class are package-private since
       * DatagramSocketImpl classes needs to access them.
       */
      byte[] buf;
      int offset;
      int length;
      int bufLength;
      InetAddress address;
      int port;
  </code></pre>
  
  <p>Doing this, the "address" field becomes the destination address, yet when we receive the package, that field corresponds to the source's.</p>
  
  <p>I think that the object itself is not sent on over the network, but its information is. </p>
  
  <p>I have briefly checked the source code of both DatagramPacket and DatagramSocket, but did not seem to find any instance where that "address" field was changed.</p>
  
  <p>My guess is that the DatagramPacket class only stores one IP because the other one is the machine's, and when the message is sent over the network, the UDP contains both IPs, which then is processed by the DatagramSocket.receive() and places the missing (source) address on the Datagram. Is that correct?</p>