>  기사  >  Java  >  Java Inet주소

Java Inet주소

PHPz
PHPz원래의
2024-08-30 15:39:39860검색

Java InetAddress는 IP 주소를 지정하는 데 사용됩니다. IP 주소는 네트워크의 기기에 할당된 고유한 숫자 레이블입니다. IP 주소는 IPv4의 경우 32비트, IPv6 주소의 경우 128비트로 지정됩니다. InetAddress의 인스턴스는 생성 중에 호스트 이름 확인이 수행되었는지 여부에 따라 호스트 이름인 IP 주소를 지정합니다. 주소에는 유니캐스트와 멀티캐스트의 두 가지 유형이 있습니다. 단일 인터페이스는 유니캐스트 주소로 식별되고, 인터페이스 세트는 멀티캐스트 주소로 식별됩니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

Java의 InetAddress 클래스는 Java의 java.net.InetAddress 패키지에 내장되어 있습니다.

InetAddress 클래스는 www.educba.com, www.google.com 등과 같은 모든 호스트의 IP 주소를 가져오는 데 사용할 수 있습니다. 일반적으로 사용되는 IP 주소는 "버전 4"의 경우 IPv4입니다. –

과 같은 IP 주소의 예를 생각해 보세요.

65.172.248.170

위 주소에는 4개의 숫자가 포함되어 있으며, 각 숫자는 '.'(점 하나)로 구분된 세 자리 숫자로 구성됩니다. 4개의 숫자 각각의 범위는 0부터 255까지입니다.

InetAddress 클래스 멤버 함수

InetAddress 클래스에는 생성자가 포함되어 있지 않지만 InetAddress 클래스 멤버 함수와 같은 일부 기능이 포함되어 있습니다.

멤버 Java InetAddress 클래스

  1. public static InetAddressgetByAddress(String Host, byte[] addr) throws UnknownHostException – 이 정적 함수는 전달된 매개변수로 호스트 이름과 IP 주소가 포함된 InetAddress 개체를 반환합니다. 호스트 매개변수는 문자열 형식의 IP 주소이거나 www.educba.com과 같은 컴퓨터 이름일 수 있습니다.
  2. public static InetAddressgetByName(String 호스트) – 이 정적 함수는 지정된 호스트의 InetAddress 개체를 반환합니다. 매개변수 호스트가 호스트 이름으로 지정된 곳입니다.
  3. public static InetAddress[] getAllByName(String 호스트) – 이 정적 함수는 지정된 호스트의 InetAddress 개체 배열을 반환합니다.
  4. public static InetAddressgetLoopbackAddress() – 이 정적 함수는 루프백 유형의 InetAddress 개체를 반환합니다.
  5. public static InetAddressgetLocalHost()가 UnknownHostException을 발생시킵니다 – 이 정적 함수는 로컬 호스트의 InetAddress 개체를 반환합니다.
  6. public byte[] getAddress() – 이 함수는 InetAddress 개체의 IP 주소를 배열로 반환합니다. 바이트순으로 배열저장된 IP 주소는 IP 주소와 같이 나타납니다.
  7. public String getHostAddress() – 이 함수는 IP 주소를 문자열 형식으로 반환합니다.
  8. public boolean isReachable(int timeout)throws IOException – 이 함수는 IP 주소에 연결할 수 있으면 true를 반환하는 반면, timeout 매개변수는 호출이 중단되고 결과가 false가 되는 시간을 지정합니다. 값입니다.
  9. public boolean isReachable(NetworkInterfacenetif, intttl, int timeout) throws IOException – 이 함수는 isReachable() 함수로 오버로드되었습니다. netif가 연결 가능성을 확인하는 데 사용되는 네트워크 인터페이스를 나타내는 경우 ttl은 네트워크를 종료하기 전 에코 패킷이 수행하는 홉 수를 나타내며, timeout 매개변수는 그 이후 시간을 지정하므로 호출이 중단됩니다.
  10. public String getHostName() – 이 함수는 IP 주소의 호스트 이름을 반환합니다.
  11. public String getCanonicalHostName() – 이 함수는 IP 주소의 정규화된 도메인 이름을 반환합니다.
  12. public String toString() – 이 함수는 IP 주소를 문자열 형식으로 나타냅니다.
  13. public boolean isAnyLocalAddress()– 이 함수는 InetAddress 개체 주소가 로컬 주소인 경우 true를 반환합니다.
  14. public boolean isLinkLocalAddress() – 이 함수는 InetAddress 개체 주소가 링크 로컬 주소인 경우 true를 반환합니다.
  15. public boolean isLoopbackAddress() – 이 함수는 InetAddress 개체 주소가 루프백 주소인 경우 true를 반환합니다.
  16. public boolean isMCGloabal() – 이 함수는 IP 멀티캐스트 주소에 전역 범위가 있는 경우 true를 반환합니다.
  17. public boolean isMCLinkLocal() – 이 함수는 IP 멀티캐스트 주소에 링크 범위가 있는 경우 true를 반환합니다.
  18. public boolean isMCNodeLocal() – 이 함수는 IP 멀티캐스트 주소에 노드 범위가 있는 경우 true를 반환합니다.
  19. public boolean isMCOrgLoacal() – 이 함수는 IP 멀티캐스트 주소에 조직 범위가 있는 경우 true를 반환합니다.
  20. public boolean isMCSiteLocal() – 이 함수는 IP 멀티캐스트 주소에 사이트 범위가 있는 경우 true를 반환합니다.
  21. public boolean isMulticastAddress() – 이 함수는 IP 주소가 처음 4비트가 1110인 멀티캐스트 주소인 경우 true를 반환합니다.
  22. public boolean isSiteLocalAddress()– 이 함수는 IPaddress가 사이트 로컬 주소인 경우 true를 반환합니다.
  23. public int hashCode() – 이 함수는 IP 주소 해시코드를 반환합니다.
  24. public boolean equals(Object obj) – 이 함수는 IP 주소가 전달된 IP 주소와 동일한 경우 true를 반환합니다.

Java의 InetAddress 클래스 예

다음으로 위에서 설명한 URL과 이 개체의 일부 함수를 사용하여 InetAddress 개체를 생성하는 다음 예제를 통해 InetAddress 클래스를 보다 명확하게 이해하기 위해 Java 코드를 작성합니다.

예시 #1

코드:

import java.io.IOException;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main( String[] arg) throws IOException
{
InetAddress ip = InetAddress.getByName("www.educba.com");
byte addr[] = { 65, 2, 0, 1};
System.out.print("iptoString : " + ip.toString());
System.out.print("\ngetAllByName : " + ip.getAllByName("www.educba.com"));
InetAddress ips[] = InetAddress.getAllByName("www.educba.com");
System.out.println("IP Address");
for (InetAddress add:ips)
System.out.println(add.getHostAddress());
// function getByName()
System.out.print("\ngetByName : " + ip);
// function getByAddress()
System.out.print("\ngetByAddress : " +InetAddress.getByAddress(addr));
// function getLocalHost()
System.out.print("\ngetLocalHost : " +InetAddress.getLocalHost());
// function getLoopbackAddress()
System.out.print("\ngetLoopbackAddress : " +InetAddress.getLoopbackAddress());
// function getAllByName() which returns all ip addresses of google.com
System.out.print("\nGoogleip addresses : " + Arrays.toString(InetAddress.getAllByName("www.google.com")));
// function isReachable()
System.out.print("\nip address isReachable : " +ip.isReachable(50));
// function getHostname()
System.out.print("\nip address hostname :" +ip.getHostName());
// function getCanonicalHostname()
System.out.print("\nip address CanonicalHostname : " + ip.getCanonicalHostName());
}
}

출력:

Java Inet주소

다음으로 InetAddress 객체에 나머지 부울 함수를 적용하는 InetAddress 클래스에 대한 Java 코드를 작성합니다.

예시 #2

코드:

import java.net.Inet4Address;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main(String[] arg) throws Exception
{
InetAddress ip =  Inet4Address.getByName("www.educba.com");
InetAddress ip1[] = InetAddress.getAllByName("www.educba.com");
byte addr[]={68, 5, 2, 12};
System.out.println("ip : "+ip);
System.out.print("\nip1 : "+ip1);
InetAddress ip2 =  InetAddress.getByAddress(addr);
System.out.print("\nip2 : "+ip2);
System.out.print("\nAddress : " +Arrays.toString(ip.getAddress()));
System.out.print("\nHost Address : " +ip.getHostAddress());
System.out.print("\nisAnyLocalAddress : " +ip.isAnyLocalAddress());
System.out.print("\nisLinkLocalAddress : " +ip.isLinkLocalAddress());
System.out.print("\nisLoopbackAddress : " +ip.isLoopbackAddress());
System.out.print("\nisMCGlobal : " +ip.isMCGlobal());
System.out.print("\nisMCLinkLocal : " +ip.isMCLinkLocal());
System.out.print("\nisMCNodeLocal : " +ip.isMCNodeLocal());
System.out.print("\nisMCOrgLocal : " +ip.isMCOrgLocal());
System.out.print("\nisMCSiteLocal : " +ip.isMCSiteLocal());
System.out.print("\nisMulticastAddress : " +ip.isMulticastAddress());
System.out.print("\nisSiteLocalAddress : " +ip.isSiteLocalAddress());
System.out.print("\nhashCode : " +ip.hashCode());
System.out.print("\n Is ip1 == ip2 : " +ip.equals(ip2));
}
}

출력:

Java Inet주소

결론

InetAddress는 java.net.InetAddress 패키지에서 사용할 수 있는 Java의 내장 클래스입니다. 네트워크에서 기기의 IP 주소를 지정하는 데 사용됩니다. 우리가 논의한 위의 방법을 사용하여 IP 주소에 대한 추가 정보를 얻을 수 있습니다.

위 내용은 Java Inet주소의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:자바 키스토어다음 기사:자바 키스토어