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));
}
}
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 |
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 |
Please disable your ad blocker and refresh the window to use this website.