title: UDP hole punching implementation
body: <p>I am trying to accomplish UDP hole punching. I am basing my theory on <a href="http://www.brynosaurus.com/pub/net/p2pnat/" rel="noreferrer">this article</a> and this <a href="http://en.wikipedia.org/wiki/UDP_hole_punching" rel="noreferrer">WIKI page</a>, but I am facing some issues with the C# coding of it. Here is my problem:</p>
  
  <p>Using the code that was posted <a href="https://stackoverflow.com/questions/9120050/connecting-two-udp-clients-to-one-port-send-and-receive">here</a> I am now able to connect to a remote machine and listen on the same port for incoming connections (Bind 2 UDP clients to the same port).</p>
  
  <p>For some reason the two bindings to the same port block each other from receiving any data.
  I have a UDP server that responds to my connection so if I connect to it first before binding any other client to the port I get its responses back.</p>
  
  <p>If I bind another client to the port no data will be received on either clients.</p>
  
  <p>Following are 2 code pieces that show my problem. The first connects to a remote server to create the rule on the NAT device and then a listener is started on a different thread to capture the incoming packets. The code then sends packets to the local IP so that the listener will get it. The second only sends packets to the local IP to make sure this works. I know this is not the actual hole punching as I am sending the packets to myself without living the NAT device at all. I am facing a problem at this point, and I don't imagine this will be any different if I use a computer out side the NAT device to connect.</p>
  
  <p>[EDIT] 2/4/2012
  I tried using another computer on my network and WireShark (packet sniffer) to test the listener. I see the packets incoming from the other computer but are not received by the listener UDP client (udpServer) or the sender UDP client (client).</p>
  
  <p>[EDIT] 2/5/2010
  I have now added a function call to close the first UDP client after the initial sending and receiving of packets only living the second UDP client to listen on the port. This works and I can receive packets from inside the network on that port. I will now try to send and receive packets from outside the network. I will post my findings as soon as I find something.</p>
  
  <p>Using this code I get data on the listening client:</p>
  
  <pre><code>static void Main(string[] args)
  {
      IPEndPoint localpt = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
  
      ThreadPool.QueueUserWorkItem(delegate
      {
          UdpClient udpServer = new UdpClient();
          udpServer.ExclusiveAddressUse = false;
          udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
          udpServer.Client.Bind(localpt);
  
          IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
          Console.WriteLine("Listening on " + localpt + ".");
          byte[] buffer = udpServer.Receive(ref inEndPoint); //this line will block forever
          Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
      });
  
      Thread.Sleep(1000);
  
      UdpClient udpServer2 = new UdpClient(6000);
  
      // the following lines work and the data is received
      udpServer2.Connect(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
      udpServer2.Send(new byte[] { 0x41 }, 1);
  
      Console.Read();
  }
  </code></pre>
  
  <p>If I use the following code, after the connection and data transfer between my client and server, the listening UDP client will not receive anything:</p>
  
  <pre><code>static void Main(string[] args)
  {
      IPEndPoint localpt = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
  
      //if the following lines up until serverConnect(); are removed all packets are received correctly
      client = new UdpClient();
      client.ExclusiveAddressUse = false;
      client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
      client.Client.Bind(localpt);
      remoteServerConnect(); //connection to remote server is done here
                             //response is received correctly and printed to the console
  
      ThreadPool.QueueUserWorkItem(delegate
      {
          UdpClient udpServer = new UdpClient();
          udpServer.ExclusiveAddressUse = false;
          udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
          udpServer.Client.Bind(localpt);
  
          IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
          Console.WriteLine("Listening on " + localpt + ".");
          byte[] buffer = udpServer.Receive(ref inEndPoint); //this line will block forever
          Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
      });
  
      Thread.Sleep(1000);
  
      UdpClient udpServer2 = new UdpClient(6000);
  
      // I expected the following line to work and to receive this as well
      udpServer2.Connect(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
      udpServer2.Send(new byte[] { 0x41 }, 1);
  
      Console.Read();
  }
  </code></pre>