Basically, I'm trying to check if a String is a valid IP address without using external libraries in Java (it doesn't have to be an IP with a working host at the other end; it can be an unused one too). I can't check if it's an IP address because I would like DNS support as well. Since the following options are valid:
- 1.2.3.4:55555
- localhost:1
- google.com:12345
- 1.2.abc:54321 (is this valid?)
but the following aren't valid:
- 1.2.3.4 (no port)
- 1.2:34 (not an IP address)
- local host:12345 (space)
- 8.8.8.8:abc (not numbers in port slot)
- stackoverflow.com:234.5 (basically only numbers should follow the :
- localhost:65536 (port number out of bounds)
At this moment (I will continue to update this), i do the following checks:
host != null;
!host.contains(" ");
host.contains(":");
Other checks I think can be done but I don't know how to do:
*Contains 1 and only 1 colon *If it only contains numbers and periods, it must contain 3 and only 3 periods, 4 numbers, and each number has to be between 0 and 255 *Number following colon is a number and is between 0 and 65535
Thank you!