title: python server-client nat traversal
body: <p>I've done some reading on UDP NAT traversal, and I'm reasonably confident I understand the basics but I'm still struggling with an implementation.</p>
  
  <p>My project has an globally accessible server, and clients behind nat. Its a game, with the basic join_game request send from client to server, and the server then sends out updates every interval. I've been testing at home, and forgotten that I have DMZ on my router turned on so it worked fine. I sent this to some friends to test and they cannot receive updates from the server.</p>
  
  <p>Here is the current methodology, all packets are UDP:</p>
  
  <ul>
  <li>Client opens a socket, and sends join request to server.</li>
  <li>Server gets the request and the reply-to address of the message, and replies to the client: <em>yes you can join, and by the way I will be sending updates to your reply-to ip/port which looks like this.</em></li>
  <li>Client catches the reply then closes the socket, and starts a UDP threaded listener class up to listen to the reply-to port the server told us about.</li>
  <li>Client then catches the server updates that get flooded over, and processes them as required. Every now and then the client opens up a new socket and sends a UDP packet with an update to the server (what keys are pressed etc).</li>
  </ul>
  
  <p>My understanding is the reply-to address that the server receives should have the correct port for traversing the client's nat. And sending packets down there often enough will keep nat traversal rule alive.</p>
  
  <p>This is not happening. The client sends the join request, and receives the server's response on that socket. But when I close the socket then start up a threaded UDP listener on the reply-to port, it doesn't catch anything. Its almost as if the traversal rule is only valid for a single response packet.</p>
  
  <p>I can include code if needed, but to be honest its several layers classes and objects and it does what I described above. The code works when I turn DMZ on, but not when its off. </p>
  
  <p>I will include some snippets of interest. </p>
  
  <p>Here is the server's handler for the join request. client_address is passed down from the threaded handler, and is the SocketServer.BaseRequestHandler attribute, self.client_address. No parsing, just passed down.</p>
  
  <pre><code>def handle_player_join(self, message, reply_message, client_address):
  
          # Create player id
          player_id = create_id()
  
          # Add player to the connected nodes dict
          self.modules.connected_nodes[player_id] = client_address
  
          # Create player ship entity
          self.modules.players[player_id] = self.modules.factory.player_ship( position     = (320, 220),
                                                                              bearing      = 0,
                                                                            )
  
          # Set reply to ACK, and include the player id and listen port
          reply_message.body                  = Message.ACK
          reply_message.data['PLAYER_ID']     = player_id
          reply_message.data['LISTEN_PORT']   = client_address[1]
  
          print "Player Joined :"+str(client_address)+", ID: "+str(player_id)
  
          # Return reply message
          return reply_message
  </code></pre>
  
  <p>A friend has mentioned that maybe when I send the join request, then get the response I shouldn't close the socket. Keep that socket alive, and make that the listener. I'm not convinced closing the socket will have any effect on the nat traversal, and I don't know how to spawn a threaded udp listener that takes a pre-existing socket without rewriting the whole damn thing (which I'd rather not).</p>
  
  <p>Any ideas or info required?</p>
  
  <p>Cheers</p>