By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,804 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,804 IT Pros & Developers. It's quick & easy.

How to find the city,state,country, latitude, longitude of the user using java

100+
P: 126
I have coded to find the ip addresses of my network. Now i need to code for finding the city,state,country, latitude, longitude with reference to the IP address, How do i do it? Is there any library or algorithms to do this using core java?
Jul 27 '13 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
What do you mean with "my network"? If you mean your home network, it does not make sense, because you know where you live. So it is your company network and it is a large company with computers in many countries. There, all IP-addresses of your computers will be given dynamically by the DNS-server and may change any time the computer is restarted, mostly in the range of 192.168.0.0 and 192.168.255.255. So you need to query the DNS-server to look up the DNS-name of the IP-Address. Then look up your company LDAP directory (ActiveDirectory) to find all data related to this DNS-name.

lookup DNS-name:
Expand|Select|Wrap|Line Numbers
  1. InetAddress addr = InetAddress.getByName("192.168.178.1");
  2. String host = addr.getHostName();
  3.  
lookup LDAP (get directory):
Expand|Select|Wrap|Line Numbers
  1. import javax.naming.directory.*;
  2. Hashtable env = new Hashtable();
  3. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  4. String url = "ldap://DNS_NAME_OR_IP_ADDRESS_OF_COMPANY_LDAP_SERVER";
  5.  env.put(Context.PROVIDER_URL, url);
  6. DirContext context = new InitialDirContext(env);
  7. ...
  8. // now lookup entries of interest, like city etc.
  9. ...
Jul 30 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.