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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
인터넷의 로컬 호스트많은 서비스가 당신에게 인터넷에서 로컬 호스트를 공개하는 방법을 제공합니다.타사 API를 테스트하거나 현재 개발 서버를 표시해야 합니다.예를 들어 가장 유행하는 것은 다음과 같다. 서버 ($5 서버 사용) 사용자 정...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.