Java 尝试仅在本地子网上进行端口扫描

Java 尝试仅在本地子网上进行端口扫描,java,networking,ip,port,subnet,Java,Networking,Ip,Port,Subnet,目前,我有一个工作端口扫描仪,但我想限制它只扫描本地子网上的IP地址 我在挖掘时找到的唯一解决方案是: 然而我不完全明白 子网和网络掩码之间有什么区别?我以为他们是一样的 其次,如何找到localHost的subnet和netmask的值 到目前为止,这就是我所拥有的 NetworkInterface network = NetworkInterface.getByInetAddress(localHost); short subnetMask = network.getInterfaceAd

目前,我有一个工作端口扫描仪,但我想限制它只扫描本地子网上的IP地址

我在挖掘时找到的唯一解决方案是: 然而我不完全明白

子网和网络掩码之间有什么区别?我以为他们是一样的

其次,如何找到localHost的subnet和netmask的值

到目前为止,这就是我所拥有的

NetworkInterface network = NetworkInterface.getByInetAddress(localHost);
short subnetMask = 
network.getInterfaceAddresses().get(1).getNetworkPrefixLength();

但这是网络掩码还是子网?如何获取其他值?

您确定的子网掩码是网络中的位数。通过将地址中位于掩码中位之后的所有位设置为零,可以找到网络地址。此代码应以通常的形式打印掩码和网络地址

NetworkInterface network = NetworkInterface.getByInetAddress(localhost);
short subnetMask
        = network.getInterfaceAddresses().get(1).getNetworkPrefixLength();
int address_int = ByteBuffer.wrap(localhost.getAddress()).getInt();
int mask_int = 0;
for(int i = 0; i < subnetMask-1; i++) {
    mask_int = mask_int | (1<<31);
    mask_int = mask_int >> 1;
}
byte mask_bytes[] =  ByteBuffer.allocate(4).putInt(mask_int).array();
InetAddress mask_address = InetAddress.getByAddress(mask_bytes);
System.out.println("mask_address = " + mask_address);
int network_int = mask_int & address_int;
byte network_bytes[] =  ByteBuffer.allocate(4).putInt(network_int).array();
InetAddress network_address = InetAddress.getByAddress(network_bytes);
System.out.println("network_address = " + network_address);
NetworkInterface network=NetworkInterface.getByInetAddress(localhost);
短子网掩码
=network.getInterfaceAddresses().get(1.getNetworkPrefixLength();
int address_int=ByteBuffer.wrap(localhost.getAddress()).getInt();
int掩码_int=0;
对于(int i=0;i
您可以使用整数形式的地址与网络掩码整数的位and来测试该网络上是否有任何地址。如果该地址在网络上,则应生成地址int