UdpClient Port binding & NAT traversal

Question about UdpClient class as a listener and NAT traversal

Page 1 of 1

4 Replies - 6584 Views - Last Post: 19 May 2010 - 02:57 PM Rate Topic: -----

#1 Mion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-April 10

UdpClient Port binding & NAT traversal

Posted 18 May 2010 - 02:47 AM

Hi guys,

I'm teaching myself C# while working on a project (personal one) which is basically a p2p messenger. The thing is I want to pretty much remove the need for a server and because of such each client effectively works AS a server and a client.

Anyway quick question regarding instantiating a UdpClient with an IPEndPoint of IPEndPoint(IPAddress.Any, 0) had a look round and search but couldn't find anything that REALLY answered my questions: -

Does this mean that a udp packet incoming from any address (and now the main point of the question) and on any port will be received by the UdpClient? And what would happen if you have two or more such UdpClient's on a single machine? Will each get a copy of the packet? Or will one UdpClient arbitrarily receive it every time? Or maybe just throw an exception on attempting to instantiate the second? Does each UdpClient instantiated this way effectively bind a random open port or some such?

Ok that wasn't such a quick question >.<

Basically I've got so far with the project coding using the socket class - realised I was borked where NAT traversal was concerned unless I was going to get into some pretty complex UPnP code then upgraded to .Net Framework 4 for the UdpClient.AllowNATTraversal() function.

Anyway I've only just started the UdpClient recode and this question has arisen so not got any code to show really, just working out the theory of how it should work in my head.

...

Sorry for rambling so much, better get on with some work work rather than my own projects >.<, any help/comments greatly appreciated :)

This post has been edited by Mion: 19 May 2010 - 02:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: UdpClient Port binding & NAT traversal

#2 Guest_Eric*


Reputation:

Re: UdpClient Port binding & NAT traversal

Posted 18 May 2010 - 12:54 PM

Hi Myon,

see http://msdn.microsof...y/k227d11f.aspx
This constructor creates a new UdpClient and binds it to the IPEndPoint specified by the localEP parameter. Before you call this constructor, you must create an IPEndPoint using the IP address and port number from which you intend to send and receive data. You do not need to specify a local IP address and port number for sending and receiving data. If you do not, the underlying service provider will assign the most appropriate local IP address and port number.

IPAddress Port:
The port number associated with the address, or 0 to specify any available port.

So creating a UdpClient with (IPAddress.Any, 0) should be like creating a UdpClient with the parameterless constructor. An IP + Port is automatically selected for the connection.

For the NAT problem (first should be the best for a personal project to learn something :>):
- create a portforwarding on your router and let all others connect to you (direct connection) http://portforward.com/
- use a public server on the net, which both clients can connect to through NAT (needs public server, not a direct connection)
- use http://en.wikipedia....P_hole_punching (not easy :>, needs public server, direct connection)
Was This Post Helpful? 0

#3 Mion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-April 10

Re: UdpClient Port binding & NAT traversal

Posted 19 May 2010 - 01:09 AM

Thanks for the reply Eric.

I'm not quite sure I understand the implications of this bit: -

Quote

You do not need to specify a local IP address and port number for sending and receiving data. If you do not, the underlying service provider will assign the most appropriate local IP address and port number.


Unless it basically means that once created you need to GET the ip/port from the UdpClient to then know where to send to to be received? I guess that saves attempting to bind a specific port and having to manually try until you find a free one.

With the NAT problem (personal project or not) I want to try and make it so the hypothetical user doesn't have to add port forwarding manually... additionally like I say - I want to avoid the need for dedicated servers sooooo... I've got an idea based on hole punching. I'll post it up if it works >.>

Thanks again.

EDIT: Ok ok I'll just post it up and maybe save myself some Dev time if it gets shot down as moronic straight away.

How about if I had a port range - say 21000 to 21100 which was for "establishing connections/punching holes" through NATs. What I mean by that is...

2 clients - A and B, both behind NAT's. A and B have communicated externally to the program (via another messenger, Text, in person) their IP's. A puts B's IP in and fires a udp packet at B across the 21000-21100 port range. B does the same shortly after. A and B bind the lowest port they successfully sent a packet out on to a receiver. A and B attempt to send confirmation packets to each other across the port range. Upon receipt of a confirmation packet they stop sending confirmation packets to the other ad retrieve the IPEndPoint from the confirmation packet, the one to receive the confirmation packet then sends a finalise packet to the IP/port it now KNOWS the other was at.

So in other words, spam open the port range on each machine where possible, setup a socket on the lowest available port in that range to receive on and await a confirmation from the other client... etc etc...

Sound like a plan or sound stupid? Starting to wonder if I posted this in the right section...

This post has been edited by Mion: 19 May 2010 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: UdpClient Port binding & NAT traversal

Posted 19 May 2010 - 08:57 AM

Hi Mion,

I think this can work, especially with low cost routers. But as NAT routers can also change the outgoing port, this will not work for 100%.
Was This Post Helpful? 0
  • +
  • -

#5 Mion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-April 10

Re: UdpClient Port binding & NAT traversal

Posted 19 May 2010 - 02:57 PM

Heh well I don't expect to get anything 100%, in the whacky world of Software Dev theres pretty much an exception to every rule ;) Thanks for the input though, its nice to have someone else that thinks it should work heh.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1