IP address with Java

How to get the IP address of a user?

InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } 

This returns: 127.0.0.1

I know this is not my IP address. This is my local IP address. How to get user IP address using java ..?

+4
source share
4 answers

The shortest way:

 try { InetAddress thisIp =InetAddress.getLocalHost(); System.out.println("IP:"+thisIp.getHostAddress()); } catch(Exception e) { e.printStackTrace(); } 

However, getLocalHost docs say:

If there is a security manager, its checkConnect method is called with the local host name and -1 as arguments to see allowed. If the operation is not permitted, an InetAddress representing the return address is returned.

and in some cases, InetAddress.getLocalHost() does not access your interfaces, it just returns the constant 127.0.0.1 (for IPv4))

I think NetworkInterface.getNetworkInterfaces is what you need to list all the features. Here is an example that does not show virtual addresses, but works for the "main" interfaces:

 import java.net.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception // Just for simplicity { for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { NetworkInterface iface = ifaces.nextElement(); System.out.println(iface.getName() + ":"); for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress address = addresses.nextElement(); System.out.println(" " + address); } } } } 

As an alternative:

Try using this (the first output should be IP after the PC name):

  InetAddress[] localaddr; String computername = null; try { computername = InetAddress.getLocalHost().getHostName();//get pc name } catch (UnknownHostException ex) { ex.printStackTrace(); } System.out.println(computername); try { localaddr = InetAddress.getAllByName(computername); for (int i = 0; i < localaddr.length; i++) { System.out.println("\n" + localaddr[i].getHostAddress()); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

References

+7
source

When you call getLocalHost() , you ask for the relative address of the router you are connected to, which (as expected) is 127.0.0.1 . To determine the IP address using InetAddress , try:

 InetAddress.getByName("http://yoururl.com/path/"); 

There is also a getAllByName(String) method that can serve your purps. Read javadoc.

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html#getHostAddress ()

+1
source

It gets the IP address of your machine,

  InetAddress inet= InetAddress.getLocalHost(); String str[]=inet.toString().split("/"); for (int i=0;i<str.length;i++) System.out.println(inet.toString() +" "+ str[i]); InetAddress[] inetAd=InetAddress.getAllByName(str[0]); for(int j=0;j<inetAd.length;j++ ){ System.out.println(inetAd[j].toString()); 
0
source

This will return your current interface address used:

 NetworkInterface ni; try { Enumeration<NetworkInterface> interfaces NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { ni = interfaces.nextElement(); if (ni.isUp()) { Enumeration<InetAddress> inetAddresses = ni.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress ia = inetAddresses.nextElement(); if (!ia.isLinkLocalAddress()) { return ia; } } } } } catch (SocketException ex) { throw new RuntimeException(ex); } 
-1
source

All Articles