title: IP headers in Java
body: <p>I am testing the behaviour of some client software, and need to write some software that emulates router-like functionality, preferably using something simple like UDP sockets. All it needs to do is receive the packet, alter the time to live, and send it back. Is this possible in regular Java? Or do you do something like the following:</p>
  
  <ol>
  <li>Listen on Socket A</li>
  <li>For EACH udp packet received, open a NEW socket, set time to live on that socket, and send it back (or this isn't possible/efficient?)</li>
  <li>Receiver gets packet with altered values that appear like it has traversed some hops (but in reality hasn't)</li>
  </ol>
  
  <p>So two approaches may be possible - editing the recieved packet directly (and then simply sending back), or constructing a new packet, copying the values from the original one and setting the appropriate headers/socket options before sending it out.</p>
  
  <p><strong>EDIT</strong>: the 'router' does not do any complex routing at all such as forwarding to other routers... it is simply decrements the t-t-l header field of the received message and sends the message directly back to the client.</p>