1 28 package org.objectweb.jonas.common; 29 30 31 import java.net.Inet4Address ; 32 import java.net.InetAddress ; 33 import java.net.NetworkInterface ; 34 import java.net.SocketException ; 35 import java.net.UnknownHostException ; 36 import java.util.Enumeration ; 37 38 39 50 public class NetUtils { 51 52 public static final String LOOP_BACK_ADDR = "127.0.0.1"; 53 54 62 public static String getLocalAddress() throws UnknownHostException { 63 64 if(InetAddress.getLocalHost().getHostAddress().equals(LOOP_BACK_ADDR)){ 66 try{ 67 NetworkInterface inter = null; 68 InetAddress addr = null; 69 Enumeration networkEnum = NetworkInterface.getNetworkInterfaces(); 71 while(networkEnum.hasMoreElements()){ 72 inter = (NetworkInterface ) networkEnum.nextElement(); 73 Enumeration enumAddr = inter.getInetAddresses(); 74 while(enumAddr.hasMoreElements()){ 76 addr = (InetAddress ) enumAddr.nextElement(); 77 if(addr instanceof Inet4Address ){ 79 if(!addr.isLoopbackAddress()){ 80 return addr.getHostAddress(); 82 } 83 } 84 85 } 86 } 87 88 return InetAddress.getLocalHost().getHostAddress(); 90 91 92 }catch(SocketException e){ 93 return InetAddress.getLocalHost().getHostAddress(); 94 } 95 }else{ 96 return InetAddress.getLocalHost().getHostAddress(); 98 } 99 } 100 101 } | Popular Tags |