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 ||new Integer(parts[i])>255)
//retroune false
return false;
}
//デフォルトではtrueを返します
return true;
}
public static void main(String[] args) {
String adr= "192.168.1.3";
System.out.println("adr+" is "+isCorrect(adr));
}
}
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/*************************************
* @author www.codeurjava.com
* All Rights Reserved
*/
public class Addressing {
public static void main(String[] zero) throws SocketException {
String address = "192.168.1.199/10";
文字列[] parts = address.split("/");
文字列 ip = parts[0];
int 接頭辞;
if (parts.length < 2) {
接頭辞 = 0;
} else {
prefix = Integer.parseInt(parts[1]);
}
System.out.println("Address =\t" + ip+"\n接頭辞 =\t" + prefix);
//マスク全体を32ビット配列に変換します
int mask = 0xffffffff <<(32 - 接頭辞);
int value = mask;
byte[] bytes_masque = new byte[]{
(byte)(値 >>>24)、(バイト)(値>>16 &0xff)、(バイト)(値>>8 &0xff), (byte)(value & 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[]{
(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[]{
(byte) (bytes_reseau[0] | bytes_masque[0]),
(byte) (bytes_reseau[1] | bytes_masque[1]),
(byte) (bytes_reseau[2] | bytes_masque[2]),
(byte) (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 <<(i*8);
}
結果を返します。
}
}
Address= 35.204.121.13 Prefix= 10 Mask= 255.192.0.0 Wildcard= 0.63.255.255 ネットワークアドレス= 35.192.0.0 ブロードキャストアドレス = 35.255.255.255 |
import java.net.InetAddress;ランタイム:
import java.net.SocketException;
import java.net.UnknownHostException;
public class ServerAddress {
public static void main(String[] zero) throws SocketException {
String LocaleAddress;
InetAddress ServerAddress;
try{
//ローカルアドレス
LocaleAdresse = (String) InetAddress.getLocalHost().getHostAddress();
System.out.println("ローカルアドレス = "+LocaleAddress);
//サーバーアドレス google
ServerAddress= InetAddress.getByName("www.google.net");
System.out.println("Googleサーバーアドレス= "+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.