Is there a public constant for the maximum TCP port number (65535) defined in java or a common library such as Apache Commons, that I could refer to from my code (instead of using the integer hardcoded)?
I'm afraid there is none you can use.
Looking at the source code of Java 8 I see the following code used by the Socket
class to verify 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 doesn't use a named constant either.
A search of the code turns up 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 isn't a public reference. Another private reference turns up in java.net.SocketPermission
.
So after the inspection above, I conclude there is none available in the Java API.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments