File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes verification of ip-addresses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "verification of ip-addresses" Watch "verification of ip-addresses" New topic

verification of ip-addresses

Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
good morning,

till now i'm using objects of InetAddress to create and verify ip-addresses. if the client enters a ip-address, i check if i can derive a new InetAddress object. if the ip is invalid ("333.0.0.0"), InetAddress will return a null value.

(my experience tells me that it is always better to work with specialized object, if they are provided)

but now i discovered that InetAddress returns null for (localhost), and probably for all ip-values below 127.x.x.x too. as far as i know this would not be correct for meeting my requirement, as a user can at least use localhost to let (one?) client and server run on the same machine, and probably also setup a local network with ip's below 127.x.x.x

how are you verifying the ip-input? are you doing this string based? or is there a better way?

many thanks for your help,
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
how are you verifying the ip-input?

I didn't, and lost nothing in that section if that means anything. If you're hell bent on doing it then maybe attempt to connect, there may be a better way but that's foolproof. Well almost, the machine may not be available when the user does the configuration. Sooooo, IMO, allow absolutely anything, I did a range check on ports, but allowed every one of them within the range, it was documented to be up to the user to know which ports they can/can't use or deal with the message and try again. On the plus side with allowing everything a user can type the name of the host instead of pinging "jan" to see the ip.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
thanks for you answer!

i decided to build a little LocalIp class, with a appropriate constructor and a isCorrect() method. less then 30 lines of code, and a big simplification for my code...

many greetings,
I agree. Here's the link:
subject: verification of ip-addresses
It's not a secret anymore!