Java 네트워크 프로그래밍 인터넷 주소 찾기

12147 단어 internet

개술


인터넷에 연결된 컴퓨터에는 인터넷 주소나 IP 주소라고 불리는 유일한 숫자가 표시되어 있다.IP가 기억하기 어려워서 사람들이 도메인네임시스템(DNS)을 설계했기 때문에 DNS는 사람들이 기억할 수 있는 호스트 이름과 컴퓨터가 기억할 수 있는 IP 주소를 연결할 수 있다.일반적으로 한 호스트는 하나의 IP 주소를 매핑합니다.호스트 이름이 여러 IP를 매핑하는 경우도 있습니다.이 때 DNS는 랜덤으로 한 대의 기계를 선택하여 요청에 응답한다. 예를 들어 업무 데이터가 매우 많은 웹 사이트는 부하를 여러 시스템으로 나눈다.

2. IP 주소란?


IP 주소는 IP가 사용하는 32자리 또는 128자리의 무기호 숫자로, UDP와 TCP 프로토콜은 모두 그 기초 위에서 구축된 것이다.

1. 주소 유형

  • 단방: 단일 인터페이스의 표지부호로 단방 주소로 보내는 데이터 패키지는 이 주소가 표시한 인터페이스에 전달된다.
  • 멀티캐스트: 하나의 인터페이스(일반적으로 서로 다른 노드에 속함)의 표지부호로 멀티캐스트 주소로 보내는 데이터 패키지는 이 주소가 표시한 모든 인터페이스에 전달된다.

  • 2. IP 범위

  • 링크 로컬: 주소 설계는 자동 주소 설정, 이웃이 라우터를 발견하거나 없을 때의 문제를 해결하기 위해 단일 링크에서 주소를 찾는 데 사용됩니다.
  • 사이트 로컬: 주소 설계는 글로벌 접두사가 필요 없을 때 사이트 내부에서 주소를 찾을 수 있도록 설계되었습니다.
  • 글로벌 주소: 인터넷에서만 사용할 수 있습니다.

  • 3. IP 주소의 텍스트 표현 형식


    IP 주소의 텍스트 표현 형식은 주소 계열에 특정합니다.
    IPV4 주소는 일반적으로 기호가 없는 네 바이트로 쓰이며, 각 바이트의 범위는 0에서 255까지이다.이런 방식을 점분 4단 포맷이라고도 부른다.eg:192.168.1.119.

    3. InetAddress 클래스


    InetAddress 클래스는 인터넷 프로토콜(IP) 주소를 나타내고 자바가 IP 주소에 대한 고급 표시입니다.다른 대부분의 네트워크 클래스에 사용됩니다.
    InetAddress 클래스의 실례는 IP 주소를 포함하고 호스트 이름도 포함할 수 있습니다. (호스트 이름으로 구성되었는지, 호스트 이름 해석을 실행했는지 여부에 따라 달라집니다.)

    1. InetAddress 객체 구성


    InetAddress 클래스에는 공통 구조 함수가 없으며, 정적 방법으로 적당히 초기화된 InetAddress 대상을 되돌려줍니다.static InetAddress[] getAllByName(String host)
    Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.static InetAddress getByName(String host)
    Determines the IP address of a host, given the host's name.static InetAddress getLocalHost()
        Returns the address of the local host.
    ps: 이 방법들은 내부 필드를 설정하는 매개 변수만 사용하는 것이 아니라 네트워크 연결을 통해 필요한 모든 정보를 얻어야 한다는 것만 지적해야 한다.이런 종류의 다른 방법은 주로 상술한 방법이 제공한 정보를 이용하여 일한다.
    DNS 검색 비용이 상대적으로 높기 때문에 InetAddress 클래스 캐시 검색 결과는networkaddress를 통해 찾을 수 있습니다.cache.ttl DNS 검색에 성공한 Java 캐시에 저장된 시간(초)을 지정합니다.InetAddress 클래스의 로컬 캐시를 제외하고 로컬 호스트, 로컬 도메인 이름 서버, 인터넷의 다른 곳의 DNS 서버도 다양한 검색 결과를 캐시합니다.

    2. 유형 방법

    boolean equals(Object obj)
    Compares this object against the specified object.byte[] getAddress()
    Returns the raw IP address of this InetAddress  object.static InetAddress[] getAllByName(String host)
    Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.static InetAddress getByAddress(byte[] addr)
    Returns an InetAddress  object given the raw IP address .static InetAddress getByAddress(String host, byte[] addr)
    Creates an InetAddress based on the provided host name and IP address.static InetAddress getByName(String host)
    Determines the IP address of a host, given the host's name.String getCanonicalHostName()
    Gets the fully qualified domain name for this IP address.String getHostAddress()
    Returns the IP address string in textual presentation.String getHostName()
    Gets the host name for this IP address.static InetAddress getLocalHost()
    Returns the address of the local host.static InetAddress getLoopbackAddress()
    Returns the loopback address.int hashCode()
    Returns a hashcode for this IP address.

    3. InetAddress 예제 코드

    public class Demo1
    {
    
        public static void main(String[] args) 
        {
            InetAddress ina;
            try
            {
                ina = InetAddress.getLocalHost();
                System.out.println(ina);
                
                System.out.println(ina.getAddress());//  InetAddress   IP 
                
                System.out.println(ina.getHostAddress());//   IP  ( )。
                
                System.out.println(ina.getHostName()); //  IP  
                
                System.out.println(ina.getLocalHost()); // 
            } 
            catch (UnknownHostException e)
            {
                // TODO   catch  
                e.printStackTrace();
            }
            InetAddress ina1;
            try
            {
                ina1 = InetAddress.getByName("192.168.1.119");
                System.out.println(ina1);
            } 
            catch (UnknownHostException e)
            {
                // TODO   catch  
                e.printStackTrace();
            }
            InetAddress[] ina2;
            try
            {
                ina2 = InetAddress.getAllByName("www.microsoft.com");
                for(int i=0;i<ina2.length;i++)
                    System.out.println(ina2[i]);
            } catch (UnknownHostException e)
            {
                // TODO   catch  
                e.printStackTrace();
            }
        }
    }

     
     
      

    좋은 웹페이지 즐겨찾기