生成随机 IP 地址
我想生成一些随机 IP 地址。但每次这个generateIPAddress函数都会返回0.0.0.0字符串作为ipAddress。但它应该每次都返回一些随机的 ipAddress,而不是 0.0.0.0。有什么建议吗?为什么会发生这种情况?
private void callingGeoService() {
int p1 = 255;
int p2 = 0;
int p3 = 0;
int inc = 5;
String ipAddress = generateIPAddress(p1, p2, p3);
p3 += inc;
if (p3 > 255) {
p3 = 0;
p2 += inc;
if (p2 > 255) {
p2 = 0;
p1--;
if (p1 <= 0) {
p1 = 0;
}
}
}
}
这是generateIPAddress 方法,
private String generateIPAddress(int p1, int p2, int p3) {
StringBuilder sb = null;
int b1 = (p1 >> 24) & 0xff;
int b2 = (p2 >> 16) & 0xff;
int b3 = (p3 >> 8) & 0xff;
int b4 = 0;
String ip1 = Integer.toString(b1);
String ip2 = Integer.toString(b2);
String ip3 = Integer.toString(b3);
String ip4 = Integer.toString(b4);
//Now the IP is b1.b2.b3.b4
sb = new StringBuilder();
sb.append(ip1).append(".").append(ip2).append(".").append(ip3).append(".").append(ip4);
// System.out.println(sb);
return sb.toString();
}
我想要以p1,p2,p3
的形式分配给ipAddress
的随机值,最后一位应该是0
。
I want to generate some random IP Address. But evertime this generateIPAddress function returns 0.0.0.0 string as ipAddress. But it should be returning some random ipAddress other than 0.0.0.0 everytime. Any suggestions why is it happening?
private void callingGeoService() {
int p1 = 255;
int p2 = 0;
int p3 = 0;
int inc = 5;
String ipAddress = generateIPAddress(p1, p2, p3);
p3 += inc;
if (p3 > 255) {
p3 = 0;
p2 += inc;
if (p2 > 255) {
p2 = 0;
p1--;
if (p1 <= 0) {
p1 = 0;
}
}
}
}
This is the generateIPAddress method
private String generateIPAddress(int p1, int p2, int p3) {
StringBuilder sb = null;
int b1 = (p1 >> 24) & 0xff;
int b2 = (p2 >> 16) & 0xff;
int b3 = (p3 >> 8) & 0xff;
int b4 = 0;
String ip1 = Integer.toString(b1);
String ip2 = Integer.toString(b2);
String ip3 = Integer.toString(b3);
String ip4 = Integer.toString(b4);
//Now the IP is b1.b2.b3.b4
sb = new StringBuilder();
sb.append(ip1).append(".").append(ip2).append(".").append(ip3).append(".").append(ip4);
// System.out.println(sb);
return sb.toString();
}
I want a random value assigned to ipAddress
in the form of p1,p2,p3
and last bit should be 0
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 Google Guava:
当然,您可以验证结果地址不进行多播等。
Using Google Guava:
of course you can validate the resulting address not to be multicast etc.
我最近开发了一个 小型库,它可以使用不同类型约束生成随机 IPv4 地址:
输出:
I've recently developed a small library that can generate random IPv4 addresses using different type constraints:
Output:
假设您实际上并不关心生成的 IP 地址以任何形式有效,那么您会遇到一个简单的问题。
用于生成地址的代码将参数
p3
和p2
设置为小于255
的值。p1
被困在255
和0
之间。真正的问题是,将其转换为地址的代码会改变这些值。
p1
为24
,p2
为16
,p3
为8< /代码>。了解调用方法中应用的限制,您可以知道
p1
、p2
和p3
永远不会超过255
,因此在这种情况下,知道8
或更多的移位将导致0
,地址的任何单个元素都不会导致以外的值0
,最后一个八位字节始终是0
,所以结果地址将是0.0.0.0
如果你想防止它是
0.0.0.0
,首先要做的是删除移位操作。这仍会将最后一个字段保留为0
,因为它从未被设置,但它应该生成地址。同样,这并不关心地址的状态,使用此方法您最终将获得广播、多播和仅限本地地址。
Assuming that you don't actually care about the resultant IP address being valid in any form whatsoever, you have a simple problem.
The code for generating the addresses sets the parameters
p3
andp2
to something less than255
.p1
is trapped between255
and0
.The real problem is that the code that turns this into an address shifts these values.
p1
by24
,p2
by16
andp3
by8
. Understanding the limitation applied in the calling method, you can know thatp1
,p2
andp3
will never exceed255
, so in this case knowing that a shift of8
or more will result in0
, none of the individual elements of the address will result in a value other than0
, and the last octet will always be0
, so the resultant address will be0.0.0.0
If you want to prevent it from being
0.0.0.0
, the first thing to do is remove the shift operations. This will still keep the last field as0
, because it is never set, but it should produce addresses.Again, this is not caring about the state of the addresses, you will end up with broadcast, multicast and local-only addresses using this method.
当您调用
generateIPAddress(p1, p2, p3)
时,p1
为255
、p2
和p3
是0
。该行将
p1
右移 24 位。轮班前,p1
为11111111
。移位结果为0
。实际上您也可以使用,因为
p1
仅打开了 8 个最低有效位。&的使用0xff
是多余的,因为该操作是在两个int
操作数之间进行的。所以b1
是0
。p2
和p3
使用值0
传递,因此移位(通过16
或8< /code>) 不会改变它一点点,导致
b2
和b3
也是0
。b4
显式设置为0
。因此,所有
b1
、b2
、b3
和b4
都是0
,从中您可以创建ip1
到ip4
。因此,方法generateIPAddress(p1, p2, p3)
始终返回0.0.0.0
。然后
p3 += inc;
将5
添加到0
。p3
中的 Reuslt 现在为5
。条件
if (p3 > 255)
始终会失败,因为p3
为5
,即<;第255章 ……
When you invoke
generateIPAddress(p1, p2, p3)
,p1
is255
,p2
andp3
are0
.This line
shifts
p1
24 bits to the right. Before the shiftp1
was11111111
. The shift results with0
. Actually you could also useas
p1
has only its 8 least significant bits turned on. The use of& 0xff
is redundant as the operation is between twoint
operands. Sob1
is0
.p2
andp3
are passed with the value0
so the shift (either by16
or8
) doesn't change it one bit, resulting withb2
andb3
being also0
.b4
is explicitly set to0
.So all
b1
,b2
,b3
andb4
are0
, from which you createip1
toip4
. So the methodgenerateIPAddress(p1, p2, p3)
always returns0.0.0.0
.Then
p3 += inc;
adds5
to0
. Reuslt inp3
now is5
.The condition
if (p3 > 255)
will always fail asp3
is5
which is< 255
...要使用子网掩码生成随机 IP 地址,请使用以下代码(请注意,在此代码中子网掩码具有 CIDR 格式):
To generate random IP address with Subnet Mask use the below code (Note that in this code Subnet Mask has CIDR format):
这个函数将帮助你生成无限数量的随机IP地址:
正如你所看到的,我生成了xx.xx.xx.xx ip格式,你可以处理它来生成任何ip格式。
this function will help you to generate an infinite number of a random IP address:
As you see i generate xx.xx.xx.xx ip format and you can handle it to generate any ip format.
我编写了一个简短的代码来过滤 wikipedia 中列出的特殊范围(稍微多一点)仅基于第一个八位位组):
I wrote a short code to filter out ranges listed in wikipedia as special (slightly more as only based on 1st octet):
生成随机 IP 地址的一种非常简单快速的方法:
A very simple and fast way to generate a random ip address: