Maximum tcp port number constant in java

Is there a public constant for the maximum TCP port number (65535) defined in java or a shared library such as Apache Commons, which I could reference my code (instead of using a single hardcode)?

+8
java tcp
source share
1 answer

I am afraid that you cannot use.

Looking at the Java 8 source code, I see the following code used by the Socket class to check for a valid port in several functions:

 private static int checkPort(int port) { if (port < 0 || port > 0xFFFF) throw new IllegalArgumentException("port out of range:" + port); return port; } 

This can be found in java.net.InetSocketAddress.checkPort(int)

As you can see, Java itself does not use a named constant.

The code search causes the following hit in java.net.HostPortrange :

 static final int PORT_MIN = 0; static final int PORT_MAX = (1 << 16) -1; 

But, as you can see, this is not a public link. Another private link appears in java.net.SocketPermission .

So, after checking the above, I came to the conclusion that there is no Java API.

+9
source share

All Articles