public class Localhost { public static void main(String[] args) throws Exception { System.out.println(InetAddress.getLocalHost().getHostAddress()); } }
andre:
mport java.net.*; import java.util.*;
public class LocalhostExt { 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()); } } } }
(af uransagelige årsager returnerer Linux 127.0.0.1 for InetAddress.getLocalHost().getHostAddress() ...)
Det skal også lige nævnes at NetworkInterface er ny i 1.4
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.