Getting the IP Address and Hostname of the Local Machine

try {
    InetAddress addr = InetAddress.getLocalHost();

    // Get IP Address
    byte[] ipAddr = addr.getAddress();

    // Get hostname
    String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}

Comments

26 Jan 2010 - 12:45pm by vgarmash (not verified)

Good example. Thank you!

28 Feb 2010 - 12:50am by Alex (not verified)

Gracias

9 Mar 2010 - 12:58pm by Anonymous (not verified)

Gracias!!!

12 Mar 2010 - 1:58pm by Anonymous (not verified)

danke szun!

16 Mar 2010 - 8:18pm by Anonymous (not verified)

This does not work on Virtual machines. Do we have any other robust way of finding machine information?

29 Mar 2010 - 3:39am by Anonymous (not verified)

Genial!

16 Apr 2010 - 9:46am by roshan india (not verified)

fine code........

22 Apr 2010 - 9:07am by andrew (not verified)

well, this gets 127.0.0.1 , what if i want to get 192.111.1.1 if i set my host ip to that ip?

25 Apr 2010 - 12:43am by Anonymous (not verified)

I want to get the address of localhost, i mean 10.0.0.2, so that i can access it from other machines.

4 May 2010 - 7:54pm by Firdi James Tommy (not verified)

thanksss

1 Jul 2010 - 7:16am by Anonymous (not verified)

try this:
String myString = addr.getHostAddress();

29 Aug 2010 - 7:08am by Ohad (not verified)

thank u !! :-)

7 Sep 2010 - 3:16pm by Al (not verified)

Excellent! Straight to the point! Thank you!!

26 Sep 2010 - 8:17am by Anonymous (not verified)

Thanks This is great to get the ip?name of server computer where application runs but what if I want the name or IP of the client that runs the application

25 Nov 2010 - 3:27pm by think01 (not verified)

It doesn't work for me. It keeps returning "127.0.1.1".

I used this code:

String hostName = InetAddress.getLocalHost().getHostName();

InetAddress addrs[] = InetAddress.getAllByName(hostName);

String myIp = "UNKNOWN";
for (InetAddress addr: addrs) {
System.out.println ("addr.getHostAddress() = " + addr.getHostAddress());
System.out.println ("addr.getHostName() = " + addr.getHostName());
System.out.println ("addr.isAnyLocalAddress() = " + addr.isAnyLocalAddress());
System.out.println ("addr.isLinkLocalAddress() = " + addr.isLinkLocalAddress());
System.out.println ("addr.isLoopbackAddress() = " + addr.isLoopbackAddress());
System.out.println ("addr.isMulticastAddress() = " + addr.isMulticastAddress());
System.out.println ("addr.isSiteLocalAddress() = " + addr.isSiteLocalAddress());
System.out.println ("");

if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
myIp = addr.getHostAddress();
}
}
System.out.println ("\nIP = " + myIp);

It finds the ip address I really need.

Hope this helps.

Cheers,

Think01

1 Feb 2011 - 11:34am by Anonymous (not verified)

How do you get the hostname using Javascript ?

18 Feb 2011 - 7:21am by Josh Warner-Burke (not verified)

This was really helpful, thanks!

p.s. if you're ever looking for stock advice check out ProofTrader (.com)

19 Feb 2011 - 2:37am by Anonymous (not verified)

This question is for testing whether you are a human visitor and to prevent automated spam submissions.

21 Feb 2011 - 2:03pm by Anonymous (not verified)

Nice addition Think01!

31 Mar 2011 - 7:02am by Amit (not verified)

Thanks You saved me precious time...

14 Jun 2011 - 1:15pm by Anonymous (not verified)

Try to use the NetworInterface class. Note that the call returns "null if there is no network interface with the specified IP address." In my tests this occurs under linux systems.

try {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
Enumeration ia = ni.getInetAddresses();
while (ia.hasMoreElements()) {
InetAddress elem = ia.nextElement();
if (elem instanceof Inet6Address) {
System.out.println("IPv6:");
} else {
System.out.println("IPv4:");
}
String str = format(" hostname: %s", elem.getCanonicalHostName());
System.out.println(str);
str = format(" address: %s", elem.getHostAddress());
System.out.println(str);
str = format(" port: %d", port);
System.out.println(str);
}
} catch (NullPointerException e) {
System.out.println("Retrieving Information from NetworkInterface failed");
}

16 Jun 2011 - 6:02am by socket (not verified)


Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface iface : Collections.list(ifaces)) {
Enumeration virtualIfaces = iface.getSubInterfaces();
for (NetworkInterface viface : Collections.list(virtualIfaces)) {
System.out.println(iface.getDisplayName() + " VIRT " + viface.getDisplayName());
Enumeration vaddrs = viface.getInetAddresses();
for (InetAddress vaddr : Collections.list(vaddrs)) {
System.out.println("\t" + vaddr.toString());
}
}
System.out.println("Real iface addresses: " + iface.getDisplayName());
Enumeration raddrs = iface.getInetAddresses();
for (InetAddress raddr : Collections.list(raddrs)) {
System.out.println("\t" + raddr.toString());
}
}

This code will list all interfaces and attached inet addresses on local machine.
Hope this helps.

Greetings
socket

14 Jul 2011 - 1:43am by Kishor (not verified)

Awesome post .. It helped me a lot.

19 Jul 2011 - 8:53pm by Aryan (not verified)

Hii, can anybody tell me how to find the ip addresses of all the nodes connected in a wireless ad-hoc network in java ........wireless ad-hoc works same as of wireless LAN..?

2 Aug 2011 - 2:47pm by Anonymous (not verified)

You Rock!

5 Aug 2011 - 10:30am by Anonymous (not verified)

Hi guys , Does anyone have an example to start the rmiregistry to a specific folder during runtime

11 Aug 2011 - 3:50am by Tanmay (not verified)

Will DNS server be queried , if i am trying to get IP address of local host

ipAddress = InetAddress.getLocalHost().getHostAddress();

22 Sep 2011 - 7:19am by ajay sinha (not verified)

can anyone tell me what is step process of intrusion detection & how it can be done by using java?

27 Sep 2011 - 10:50pm by V.mohanraj (not verified)

thanks....

3 Nov 2011 - 10:45am by Joe (not verified)

That poem is stupid and it doesn't belong here.

20 Mar 2012 - 11:00pm by abhi (not verified)

i need the code for finding ip address of client machine and using the ip address we need to authenticate tat client and send data back as confirmed.
pls do needfull

1 Apr 2012 - 8:32am by Touhid (not verified)

pls Help me to serch all ip address of PCs connected by LAN(JAVA code)

4 Apr 2012 - 9:26pm by Werner JQ (not verified)

public static String getIPAddress() {
Enumeration nicList;
NetworkInterface nic;
Enumeration nicAddrList;
InetAddress nicAddr;
try {
nicList = NetworkInterface.getNetworkInterfaces();
while (nicList.hasMoreElements()) {
nic = nicList.nextElement();
if (!nic.isLoopback() && nic.isUp()) {
nicAddrList = nic.getInetAddresses();
while (nicAddrList.hasMoreElements()) {
nicAddr = nicAddrList.nextElement();
try {
Inet4Address nicAddrIPv4 = (Inet4Address) nicAddr; //test if it's IPv4, if doesn't throws Exception.
return nicAddr.getHostAddress();
} catch (Exception e) {
}
}
}
}
} catch (SocketException e1) {
System.out
.println("SocketException handled in Networking.getIPAddress!.");
}
return "";
}

10 May 2012 - 7:08am by Anonymous (not verified)

sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsd
sdfasdfsdsdfasdfsd
sdfasdfsd
sdfasdfsd

15 May 2012 - 8:53am by personal loans (not verified)

If you are willing to buy real estate, you will have to get the loans. Furthermore, my mother always utilizes a bank loan, which seems to be the most rapid.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.