0

I'm trying to figure out how the Android OS assigns ports via the ServerSocket constructor.

(Link: http://developer.android.com/reference/java/net/ServerSocket.html)

Specifically, using the 2nd constructor, the documentation claims that "If port == 0, a port will be assigned by the OS." Does anyone know how this port is assigned by the Android OS? Based on running a significant amount of netstat -a commands on the Android terminal, it appears to be a random selection.

Can anyone confirm this? I'd like to know what I can expect when a 0 is entered for this parameter.

Thanks in advance!

Siege898
  • 463
  • 4
  • 10
  • 1
    I don't know the specifics, but seeing as Android is Linux underneath, this may be informative: https://idea.popcount.org/2014-04-03-bind-before-connect/#port-allocation – Barend Aug 26 '14 at 18:01

1 Answers1

1

As per java documentation:

A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range.

Android documentation suggests that OS chooses the port for you:

If port == 0, a port will be assigned by the OS

Basically it will choose a random free port.

Some extra: This site that contains a list of ports and service who use it.

Simas
  • 43,548
  • 10
  • 88
  • 116