2010-03-09 5 views

답변

56

(예를 들어 127.0.0.1)과 주소를 모든 인터페이스에 할당 된 것과 같은.

가 에게
boolean isMyDesiredIp = false; 
try 
{ 
    isMyDesiredIp = isThisMyIpAddress(InetAddress.getByName("192.168.220.25")); //"localhost" for localhost 
} 
catch(UnknownHostException unknownHost) 
{ 
    unknownHost.printStackTrace(); 
} 
+0

("IP 여기에 온다") InetAddress.getHostName해야한다고 ... getbyHostName이 존재하지 않는 : 예를 들어 ... 문자열로

public static boolean isThisMyIpAddress(InetAddress addr) { // Check if the address is a valid special local or loop back if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true; // Check if the address is defined on any interface try { return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false; } } 

포트를 나타내는, 이것을 호출 . –

+3

@AbhijeetKashnia : 고마워,하지만 실제로는'InetAddress.getByName()','getHostName()'은 클래스 메쏘드가 아니라 객체 메쏘드이고 뭔가 다른 것을한다. (IP 주소의 호스트 이름을 얻는다. DNS 역방향 조회 사용). –

관련 문제