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 整数(parts[i])>255)
//retroune false
返回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
* 保留所有权利
*/
public class 寻址 {
public static void main(String[] zero) 抛出 SocketException {
String address = 192.168.1.199/10”;
String[] parts = address.split(/”);
字符串 ip = parts[0];
int 前缀;
if (parts.length < 2) {
prefix = 0;
} else {
prefix = Integer.parseInt(parts[1]);
}
System.out.println(地址 =\t” + ip+\n前缀 =\t” + 前缀);
//将整个掩码转换为 32 位数组
int mask = 0xffffffff <<(32 - 前缀);
int 值 = 掩码;
byte[] bytes_masque = new byte[]{
(byte)(值 >>>24)、(字节)(值 >>16 &0xff)、(byte)(值>>8 &0xff), (byte)(值 & 0xff) };
try {
//masque
InetAddress netAddr = InetAddress.getByAddress(bytes_masque);
System.out.println(掩码 =\t” + netAddr.getHostAddress());
/*************************
* 网络地址
*/
//将IP地址转换为long
long ipl = ipToLong(ip);
//将 IP 转换为 32bits
byte[] 数组 bytes_ip = new byte[]{
(byte) ((ipl >>24) &0xFF),
(byte) ((ipl >>16) &0xFF),
(byte) ((ipl >>8 ) &0xFF),
(byte) (ipl & 0xFF)};
//IP地址和mask
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
//反转 mask
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 通配符= 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 服务器地址;
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.