IntelliJ Java IDE
The moose likes Sockets and Internet Protocols and the fly likes Easy way to verify an IP #? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Easy way to verify an IP #?" Watch "Easy way to verify an IP #?" New topic
Author

Easy way to verify an IP #?

Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Is there an easy way to verify that an IP number the user entered is valid in form? I mean, I know that you can parse the string and check for 3 periods and that there aren't more than 3 numbers between periods, but I was wondering if there was a simpler way.
I tried using InetAddress.getByName(String), but getByName will actually alter the String it is passed to make it fit! (ex: 121.0.55 becomes 121.0.0.55, 121 becomes 0.0.0.121)


There's nothing more dangerous than a resourceful idiot. ---Dilbert
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
not any that i know of.
i did it by splitting it at the '.' and then check if the values are 0 <= value <= 255 and there are four of them. this should be ebough. care must be taken also if a port is entered...split the string again at the ':' and check if an integer is located behind (bettween 1 and 2^16-1 -i think)
k
Joel Peach
Greenhorn

Joined: Oct 12, 2001
Posts: 19
Someone should really write a component to do this. There are actually many more numeric representations that are allowed for specifying IP addresses, including different bases (octal, hex, etc).
See the following site for more information on the alternate IP address representations.
http://www.pc-help.org/obscure.htm
Best of luck!
-Joel
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
nice link....didnt know this.
however the dotted op format is the one used everywhere and i dont see a need in supporting obscure "features" as dWord representation. or is there a reason ?
k
 
 
subject: Easy way to verify an IP #?
 
Threads others viewed
round method
Alias name for ip
verification of ip-addresses
How to keep validation for IP addresses
SOAPConnection call with ip address
IntelliJ Java IDE