Java 中的网络 - IP 地址

每台机器都有一个 IP 地址网络中独一无二的。它用于识别机器,以便它可以接收使用其他机器发送的路由器路由的数据包。
IPV4 on 32 位(4 字节)的 IP 地址采用以下格式:xxx.xxx.xxx.xxx,例如:192.168.0.3。它由两部分组成: 
右侧部分表示名为 网络 ID  : 192.168.0,它是静态的.
左边的第二部分显示了这个网络中的机器,称为 主机 ID  : 192.168.0.3.
默认IP地址为0.0.0.0/8。它仅用于连接到 DHCP 服务器,后者提供IP地址.

有三类寻址:
  1. 类 A: 网络 ID:xxx(126 个网络)和 主机 ID: xxx.xxx.xxx (16777214 computers).
  2. b类: 网络 ID : xxx.xxx (16384网络 ) et l'主机 ID: xxx.xxx (65534 台计算机).
  3. 类C: 网络 ID : xxx.xxx.xxx (2097152网络) et l'主机 ID: xxx (254 计算机).

Mask:

兴趣 的面具是允许 识别 与 IP 地址关联的网络。它由 4 个字节组成,例如一个 IP 地址,例如 IP 地址 192.168.0.1 的掩码是 255.255.255.0,我们写 192.168.0.1/24。对于初学者,我建议您在维基百科上阅读此摘要:IP address.

检查IP地址是否正确

public class VerifierAddress {

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));
}
}
允许我们操作 IP 地址的软件包是 java.net并提供类 InetAdress,其中包含我们将要使用的方法

getLocalHost:返回机器的本地 IP 地址。
getByName(String nom_de_la_machine):返回与作为参数传递的名称匹配的 IP。
getAllByName(String nom_de_la_machine):返回所有 IP 地址。与作为参数传递的名称匹配.

任何描述的方法返回的适用于此对象的方法有:
getHostName:返回机器的名称。
getAddress:以 array.
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) 抛出 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

知道服务器在 web 上的 IP 地址

也可以从网址中找出计算机和服务器的本地地址。

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
Advertisement

AdBlock Detected

Please disable your ad blocker and refresh the window to use this website.