user3432970
user3432970

Reputation: 1

Detect if an IP is local or public

I have a Java server where I need to check if an IP is local or public when I get a connection. I can detect it when its my own local IP but I'm having some trouble with other local IPs. Does Java give us a way to do it? I haven't found it, so probably we should convert IP to numbers and check if is in the local IP range. How can I get this range?

Upvotes: 0

Views: 2754

Answers (3)

user2936450
user2936450

Reputation:

The private IP ranges are:

10.0.0.0/8 (from 10.0.0.0 to 10.255.255.255) [ Named as Class A Private Network ]

172.16.0.0/12 (from 172.16.0.0 to 172.31.255.255) [ Named as Class B Private Network ]

192.168.0.0/16 (from 192.168.0.0 to 192.168.255.255) [ Named as Class C Private Network ]

Upvotes: 2

TheEwook
TheEwook

Reputation: 11117

An IP address within one of three ranges are used within local networks only (behind routers and firewalls) and considered as private IP address ranges.

  • 10.0.0.0 - 10.255.255.255
  • 172.16.0.0 - 172.31.255.255
  • 192.168.0.0 - 192.168.255.255

I don't think Java provide such a way to tell if an IP address is private or public. I think you have to write your own function.

Upvotes: 0

Stunner
Stunner

Reputation: 1151

You need to elaborate your question. From my understanding you want to check whether the IP lies inside 192.x.x.x or outside the range. If that is the case just a string or integer comparision will solve your question.

Upvotes: 0

Related Questions