3

This is the first time I'm working with IPEndPoint, so pardon my lack of experience.

If I create an IPEndPoint like this:

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.25"), 0);

Is it possible that port 0 will already be in-use such that this will produce an exception? Is there any type of convention here for which local port number to use? Should I just use a randomly generated number that's less than 65,536? Is there a best or safest approach to choosing a port and does it even matter?

The use case in this particular instance is just for me to learn more. I'm fiddling with sending UDP broadcast WOL packets at the moment, but later I expect to get into a bit more complicated code. I just want to make sure I understand best practice.

crthompson
  • 15,653
  • 6
  • 58
  • 80
blitz_jones
  • 1,048
  • 2
  • 10
  • 22

2 Answers2

3

First, you would want to check to see if you are interfering with any standard windows service ports

Random in that case would be a very bad way to go, the port may be in use by some other app.

But you can always go here and choose something that is unassigned.

There, the IANA says

The Dynamic and/or Private Ports are those from 49152 through 65535

crthompson
  • 15,653
  • 6
  • 58
  • 80
  • I understand the IANA documentation. What I don't understand is exactly when it really matters and when it doesn't. What I mean to say is that suppose I choose port 49153, but Joe Coder also wrote something using 49153. If I need to bind to a local endpoint for a moment to send a UDP packet, will I get an exception if Joe Coder's app is already running? If my app needs only to send UDP packets now and then, would I be better off choosing a randomly generated port number between 49152 and 65535 so that I can retry on failure with a new port number rather than sticking with a static 49153? – blitz_jones Dec 06 '13 at 22:41
3

According to IANA

Port numbers are assigned in various ways, based on three ranges: System Ports (0-1023), User Ports (1024-49151), and the Dynamic and/or Private Ports (49152-65535); the difference uses of these ranges is described in [RFC6335]. System Ports are assigned by IETF process for standards-track protocols, as per [RFC6335]. User Ports are assigned by IANA using the "IETF Review" process, the "IESG Approval" process, or the "Expert Review" process, as per [RFC6335]. Dynamic Ports are not assigned.

Also from the UdpClient(port) ctor MSDN documentation:

If you pass 0 to the constructor, the underlying service provider will assign a port number.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • 1
    I understand the IANA documentation. What I don't understand is exactly when it really matters and when it doesn't. What I mean to say is that suppose I choose port 49153, but Joe Coder also wrote something using 49153. If I need to bind to a local endpoint for a moment to send a UDP packet, will I get an exception if Joe Coder's app is already running? If my app needs only to send UDP packets now and then, would I be better off choosing a randomly generated port number between 49152 and 65535 so that I can retry on failure with a new port number rather than sticking with a static 49153? – blitz_jones Dec 06 '13 at 22:46
  • If you are just sending then don't use the UdpClient actor that takes an IPEndPoint. That one is only useful for receiving. Just use the parameterless actor or the one that takes an AddressFamily. – Sam Axe Dec 06 '13 at 22:49
  • The IP stack will manage your local port if all you are doing is sending. – Sam Axe Dec 06 '13 at 22:50
  • 1
    I was doing that initially, but then I discovered that when sending WOL packets to 255.255.255.255 you have to individually bind to each adapter and send the packet to make sure you get your packet sent on the correct network. The automatic selection that is done for you when you don't specify an IPEndPoint is not sufficient in this case since it might send to a network that you don't care about. – blitz_jones Dec 06 '13 at 22:55
  • DOH! I see now that the MSDN documentation for the IPEndPoint constructor also states that you can use 0 to specify any available port! Not sure how I missed that. Thanks! – blitz_jones Dec 06 '13 at 22:59
  • 1
    Ah, right you are. Well I don't know the correct answer in this case. But in the time it takes someone to post the correct answer you could just fire up 2 or more copies of your program and find the answer yourself. – Sam Axe Dec 06 '13 at 22:59
  • @dizzy.stackoverflow Sorry guys, stepped away from my machine for a minute. Great discussion and answer. +1 – crthompson Dec 06 '13 at 23:04
  • @paqogomez - no worries at all. Thank you for your contribution. – blitz_jones Dec 06 '13 at 23:06