• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to keep validation for IP addresses

 
Ravi Verma
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!!
i have one requirement i am running one ServerSocket which accepts connections and i have to verify the incoming connection IP Address should be in the 192.168.1.1 to 192.168.1.20
Is there any way i can specify the range if IP with Masking basically i should compare the Incoming Request Ip to my permitted ip list.
Note: no string comparisions pls
Thanks in advance
Ravi
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Socket.getInetAddress().getAddress() to return the 4 byte IP address. Use it on the Socket object that comes from ServerSocket.accept(), don't be confused by ServerSocket.getInetAdress(), which should return the address of your local server.
If you were looking to allow only IP addresses from within a particular sub-net, you could then AND these 4 bytes with the sub-net mask, and see if you are left with the 4 byte sub-net address.
Otherwise, you need to be more specific on what you mean when you say 'specify a range of IP addresses'. AFAIK, there is no general API in Java that implements IP filtering.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!