how can I know the IP of the local computer

L

lrantisi

Dear friends
how can I know the IP of the local computer, using java InetAddress
maybe or another object.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

lrantisi said:
how can I know the IP of the local computer, using java InetAddress
maybe or another object.

Simple:

import java.net.*;

public class Localhost {
public static void main(String[] args) throws Exception {
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}

more advanced:

import java.net.NetworkInterface;
import java.net.InetAddress;
import java.util.Enumeration;

public class NI15 {
public static void main(String[] args) throws Exception {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
System.out.println("Net interface: " + ni.getName());
Enumeration e2 = ni.getInetAddresses();
while (e2.hasMoreElements()){
InetAddress ip = (InetAddress)e2.nextElement();
System.out.println("IP address: " + ip.getHostAddress());
}
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top