Java의 네트워크 - IP 주소
각 컴퓨터에는 IP 주소 네트워크에서 고유합니다. 다른 기기에서 보낸 라우터를 사용하여 라우팅된 패킷을 수신할 수 있도록 기기를 식별하는 데 사용됩니다.a href="http://fr.wikipedia.org/wiki/Transition_d%27IPv4_vers_IPv6">IPV4 on 32비트(4바이트) 유형의 IP 주소는 xxx.xxx.xxx.xxx 형식입니다(예: 192.168.0.3). 두 부분으로 구성되어 있습니다.
오른쪽 부분은 네트워크 ID : 192.168.0이고 static입니다.
getLocalHost: 컴퓨터의 로컬 IP 주소를 반환합니다.
getByName(String nom_de_la_machine): 매개변수로 전달된 이름과 일치하는 IP를 반환합니다.
getAllByName(문자열 nom_de_la_machine): 모든 IP 주소를 반환합니다. 매개 변수로 전달된 이름과 일치합니다.
설명된 메서드 중 하나에서 반환된 이 개체에 적용할 수 있는 메서드는 다음과 같습니다.
getHostName: 머신의 이름을 반환합니다.
getAddress: IP 주소를 배열로 반환합니다.
toString: 거기로 돌아가기 채널 이름 및 주소 포함.
다음 프로그램은 접두사, 주소, 넷마스크, Cisco 와일드카드, 네트워크 주소 및 브로드캐스트 주소를 계산합니다.
주소 지정에는 세 가지 클래스가 있습니다.
- 클래스 A: 네트워크 ID: xxx(126개 네트워크) 및 호스트 ID: xxx.xxx.xxx (16777214 컴퓨터).
- 클래스 B: 네트워크 ID : xxx.xxx (16384 네트워크 ) et l'호스트 ID: xxx.xxx (65534 컴퓨터).
- 클래스 C: 네트워크 ID : xxx.xxx.xxx (2097152 네트워크 ) et l'호스트 ID: xxx (254 컴퓨터).
마스크:
이자 마스크의 허용 식별하려면 IP 주소와 연결된 네트워크입니다. 다음과 같은 4 바이트로 구성됩니다. IP 주소, 예를 들어 IP 주소 192.168.0.1의 마스크는 255.255.255.0이고 192.168.0.1/24를 씁니다. 초보자의 경우 Wikipedia에서이 요약을 읽는 것이 좋습니다 : IP 주소.IP 주소가 올바른지 확인
public class VerifierAddress {IP 주소를 조작할 수 있는 패키지는 java.net InetAdress는 우리가 작업 할 메소드를 포함합니다 :
static boolean isCorrect(String adr){
String[] parts = adr.split("\\.");
//숫자의 범위는 0에서 255 사이입니다
for(int i = 0 ; i < 4; i++){
//정수로 변환하고 테스트
if(new Integer(parts[i])< 0 || 새로운 정수 (부품 [i])> 255)
//retroune false
false를 반환합니다.
}
//기본적으로 true를 반환합니다
true를 반환합니다.
}
public static void main(String[] args) {
문자열 adr= "192.168.1.3";
System.out.println("adr+"는 "+isCorrect(adr));
}
}
getLocalHost: 컴퓨터의 로컬 IP 주소를 반환합니다.
getByName(String nom_de_la_machine): 매개변수로 전달된 이름과 일치하는 IP를 반환합니다.
getAllByName(문자열 nom_de_la_machine): 모든 IP 주소를 반환합니다. 매개 변수로 전달된 이름과 일치합니다.
설명된 메서드 중 하나에서 반환된 이 개체에 적용할 수 있는 메서드는 다음과 같습니다.
getHostName: 머신의 이름을 반환합니다.
getAddress: IP 주소를 배열로 반환합니다.
toString: 거기로 돌아가기 채널 이름 및 주소 포함.
다음 프로그램은 접두사, 주소, 넷마스크, Cisco 와일드카드, 네트워크 주소 및 브로드캐스트 주소를 계산합니다.
import java.math.BigInteger;
import java.net.InetAddress를 사용합니다.
import java.net.SocketException;
import java.net.UnknownHostException;
/*************************************
* @author www.codeurjava.com
* 판권 소유
*/
public class 주소 지정 {
public static void main(String[] zero) throws SocketException {
String address = "192.168.1.199/10";
문자열[] 부품 = address.split("/");
문자열 ip = 부품[0];
int 접두사;
if (parts.length < 2) {
접두사 = 0;
} else {
접두사 = Integer.parseInt(parts[1]);
}
System.out.println("주소 =\t" + ip+"\n접두사 =\t" + 접두사);
//전체 마스크를 32비트 배열로 변환
int mask = 0xffffffff < < (32 - 접두사);
int 값 = 마스크;
byte[] bytes_masque = new byte[]{
(바이트)(값 > > > 24), (바이트)(값 > > 16 & 0xff), (바이트)(값 > > 8 & 0xff), (바이트)(값 & 0xff) };
try {
//masque
InetAddress netAddr = InetAddress.getByAddress(bytes_masque);
System.out.println("마스크 =\t" + netAddr.getHostAddress());
/*************************
* 네트워크 주소
*/
//IP 주소를 long
long ipl = ipToLong(ip);
//IP를 32비트로 변환
byte[] 배열 bytes_ip = new byte[]{
(바이트) ((ipl > > 24) & 0xFF),
(바이트)((ipl > > 16) & 0xFF),
(바이트)((ipl > > 8 ) & 0xFF),
(바이트) (ipl & 0xFF)};
//IP 주소와 마스크 사이의 논리적 ET입니다
byte[] bytes_reseau = new byte[]{
(byte) (bytes_ip[0] & bytes_masque[0]),
(byte) (bytes_ip[1] & bytes_masque[1]),
(byte) (bytes_ip[2] & bytes_masque[2]),
(바이트) (bytes_ip[3] & bytes_masque[3]),
};
//네트워크 주소 획득
InetAddress adr_reseau = InetAddress.getByAddress(bytes_reseau);
System.out.println("네트워크 주소 =\t"+adr_reseau.getHostAddress());
/********************************
*브로드캐스트 주소
*/
//네트워크 주소 - 반전 마스크 ~val & 0xff
//마스크 반전
bytes_masque = new byte[]{
(byte) (~bytes_masque[0] & 0xff),
(byte) (~bytes_masque[1] & 0xff),
(byte) (~bytes_masque[2] & 0xff),
(byte) (~bytes_masque[3] & 0xff),
};
System.out.println("와일드카드 마스크) =\t"+InetAddress.getByAddress(bytes_masque).getHostAddress());
byte[] bytes_broadcast = new byte[]{
(바이트) (bytes_reseau[0] | bytes_masque[0]),
(바이트) (bytes_reseau[1] | bytes_masque[1]),
(바이트) (bytes_reseau[2] | bytes_masque[2]),
(바이트) (bytes_reseau[3] | bytes_masque[3]),
};
//브로드캐스트 주소 획득
InetAddress adrbroadcast = InetAddress.getByAddress(bytes_broadcast);
System.out.println("브로드캐스트 주소 =\t"+adrbroadcast.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static long ipToLong(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
결과 |= IP < < (나*8);
}
결과 반환;
}
}
런타임:
주소 = < / span> 35.204.121.13 접두사= 10 mask= 255.192.0.0 와일드 카드 = < / span> 0.63.255.255 네트워크 주소= 35.192.0.0 브로드캐스트 주소 = 35.255.255.255 |
웹에서 서버의 IP 주소를 알고 있습니다
웹 주소에서 컴퓨터와 서버의 로컬 주소를 찾을 수도 있습니다.import java.net.InetAddress;런타임 : < / span >< / div>
import java.net.SocketException;
import java.net.UnknownHostException;
public class ServerAddress {
public static void main(String[] zero) throws SocketException {
String LocaleAddress;
InetAddress 서버 주소;
try{
//로컬 주소
LocaleAdresse = (문자열) InetAddress.getLocalHost().getHostAddress();
System.out.println("로컬 주소 = "+LocaleAddress);
//서버 주소 google
ServerAddress= InetAddress.getByName("www.google.net");
System.out.println("구글 서버 주소 = "+ServerAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
로컬 주소 = 192.168.1.2 Google 서버 주소 = www.google.net/41.201.128.40 |