【发布时间】:2015-02-17 01:20:03
【问题描述】:
我必须获取路由器/网关 IP,然后每次都增加最后一个八位字节以扫描所有设备。我使用函数 NetworkGateway() 获取网关 IP。
//gets the GateWay IP
string gate_ip= NetworkGateway();
//mechanism to parse ,get the last octet and increment it everytime goes here so that ping_var = "192.168.0" + "." + i; have a fixed "192.168.0"
for (int i = 2; i <= 255; i++)
{
//changes i for every unique device
string ping_var = "192.168.0" + "." + i;
}
IPAddress address = IPAddress.Parse(ipAddress);
// gives 192.168.0.1 when passed "192.168.0.1 "
Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());
//Returns the Bytes of IP
Byte[] bytes = address.GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
Console.Write(bytes[i]);
}
为此,我首先使用了IPAddress.Parse(ipAddress),然后使用了GetAddressBytes(),但它只返回了xxxxxxxx,它不能用于递增。
AddOne 方法建议的here at SO 也没有返回。
任何好主意。
【问题讨论】:
-
相关代码在哪里?
IPAddress.Parse不是一种新方法,如果它不起作用,人们会在十年前注意到它。还有xxx是什么意思? `GetAddressBytes 返回一个字节数组,而不是字符串。 -
更新了上面的代码
-
GetAddressBytes()给19216801传递给它一个参数192.168.0.1。我可以做什么19216801来获得要求 -
正如我所说,GetAddressBytes 返回一个字节数组。
19216801不是字节数组。您还没有发布相关代码,所以帮助您非常困难。您可以轻松地将最后一个字节的值从 1 更改为 255,并从数组中创建一个新的 IPAddress -
好的,我做到了并作为答案发布
标签: c# ip-address