title: UDP allocating a new socket for each new connection
body: <p>Here is my setup: On my home network a server is running on port <code>SERVER_PORT</code>. A port forwarding rule has been added to the router such that all incoming connections to <code>SERVER_PORT</code> will reach the server's machine. Somewhere else on the internet there exists a client who tries to send a UDP packet to the server and receive a packet back.</p>
  <p>The server will receive the connection on <code>SERVER_PORT</code> but will send a packet back from a socket bound to a different (random) port.</p>
  <p>The idea is emulating a listener socket just like the one used for TCP, and for each new connection allocating a new socket.</p>
  <p>I have tested my program with clients that are connected to my home network, and outside the home network (cellular). The program didn't work either way.</p>
  <p>The program does work though if I send a packet back using the server's original socket (the one that is bound to <code>SERVER_PORT</code>).</p>
  <p>I am assuming this has something to do with my router's NAT table but I can't quite figure out the details.</p>
  <p>My question is how can a packet be sent back from a different socket, other than the one that had been used for previous packets, and would I have had the same issue if the server was directly connected to the internet?</p>
  <p>I wrote a small java program to demonstrate the issue:</p>
  <p>Here is the server's code:</p>
  <pre><code>    //create both sockets and a buffer
      DatagramSocket serverSocket1 = new DatagramSocket(SERVER_PORT);
      DatagramSocket serverSocket2 = new DatagramSocket();
      byte[] buffer = new byte[512];
  
      //receive a packet from serverSocket1
      DatagramPacket packet1 = new DatagramPacket(buffer, buffer.length);
      serverSocket1.receive(packet1);
      System.out.println(&quot;received packet from: address = &quot; + packet1.getAddress() + &quot;, port = &quot;+ packet1.getPort());
  
      //send a packet from serverSocket2
      DatagramPacket packet2 = new DatagramPacket(buffer, buffer.length, packet1.getAddress(), packet1.getPort());
      serverSocket2.send(packet2);
      System.out.println(&quot;sent packet back&quot;);
  </code></pre>
  <p>Here is the client's code: (note: <code>SERVER_ADDRESS</code> is set to my public ip)</p>
  <pre><code>    //create a socket for the client and a buffer
      DatagramSocket clientSocket = new DatagramSocket();
      byte[] buffer = new byte[512];
  
      //send packet1
      DatagramPacket clientPacket1 = new DatagramPacket(buffer, 0, buffer.length, InetAddress.getByName(Server.SERVER_ADDRESS), Server.SERVER_PORT);
      clientSocket.send(clientPacket1);
      System.out.println(&quot;sent packet&quot;);
  
      //receive packet2
      DatagramPacket clientPacket2 = new DatagramPacket(buffer, buffer.length);
      clientSocket.receive(clientPacket2);
      System.out.println(&quot;received packet from: address = &quot; + clientPacket2.getAddress() + &quot;, port = &quot;+ clientPacket2.getPort());
  </code></pre>
  <p>After running first the server and the the client I get the following output:</p>
  <p>server:</p>
  <pre><code>received packet from: address = /107.107.56.147, port = 29098
  sent packet back
  </code></pre>
  <p>client:</p>
  <pre><code>sent packet
  </code></pre>