Maximum tcp port number constant in java

dammkewl :

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)?

Thirler :

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.

edited at
0

Comments

0 comments
Login to comment

Related

Maximum port number used for bind() fun call

Why is the maximum port range 65535 in the TCP/IP Suite?

Maximum TCP connection duration generally or in java?

Maximum number of dimensions in a Java array

Java - Printing maximum number of variables

Maximum number of enum elements in Java

Java - Maximum number of type parameters

Logging source tcp port when ConnectException in Java?

Change Java spark port number

Benchmark maximum number of TCP connections on Windows Server 2012

Oneliner command to use kill given tcp port number instead of PID?

Why UDP and TCP use port number but not PID to locate the destination?

Python socketserver, how to get my own tcp port number

TCP server takes wrong port number on ubuntu 14.04

PCAP - I'm not capturing the right TCP port number

Getting SQL Server instance name from tcp port number

Traceroute number of hops changes depending on the tcp port used

Java - double constant number of decimal places

java Hashmap : Get the key with maximum number of values

Find the minimum and maximum number in an Array in Java

Maximum number of local variables in a Java Method

Maximum number of parameters in Java method declaration

java how to stop when maximum number reached

Issue with java array not identifying maximum number

Error in double maximum number calculation program in java

maximum length or constant

Is there a constant for the maximum CGFloat value?

Is there a well defined default port number to be used as a Java RMI registry port

Remove port number from a url in java