更改 gethostentry 返回的 IP 的最后一个八位字节

发布于 2024-12-06 19:02:10 字数 688 浏览 0 评论 0原文

想知道是否有人可以帮助我。我对 C# 不太了解,但它对于我想做的事情来说很容易。

我正在制作一个小型应用程序,它将接收网络上的主机名,然后返回完整的 ip 地址(ipv4)...。从那里我可以选择 ping/vnc/telnet...等。

我的问题就在这里...我正在使用 GetHostEntry 返回 IP 地址。然后我想将 IP 存储到一个变量中,并更改最后一个八位字节。我认为一个简单的 sting.split('.') 就是答案,但我无法将 IP 转换为字符串,因为源不是字符串。有什么想法吗?

这是我获取 IP 地址的方法,它只是基本的 GetHostEntry 方法:

IPHostEntry host = Dns.GetHostEntry( hostname );

Console.WriteLine( "GetHostEntry({0}) returns: {1}", hostname, host );

// This will loop though the IPAddress system array and echo out
// the results to the console window

foreach ( IPAddress ip in host.AddressList )
{
    Console.WriteLine( "    {0}", ip );
}

Was wondering if anyone could help me out here. I don't dont much with c# but its easy for what im trying to do.

I am making a small application that will take in the hostname on my network and then return the full ipaddress(ipv4) ....From there i have options to ping/vnc/telnet...etc.

My question lies here... I am using GetHostEntry to return the ip address. Then I want to store the IP into a variable, and change the last octet. I figured a simple sting.split('.') would be the answer, but I can't convert the IP to a string because the source is not a string. Any ideas?

Here is my Method to get the IP address, its just the basic GetHostEntry method:

IPHostEntry host = Dns.GetHostEntry( hostname );

Console.WriteLine( "GetHostEntry({0}) returns: {1}", hostname, host );

// This will loop though the IPAddress system array and echo out
// the results to the console window

foreach ( IPAddress ip in host.AddressList )
{
    Console.WriteLine( "    {0}", ip );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-12-13 19:02:10

假设只有一个网络适配器:

// When an empty string is passed as the host name, 
// GetHostEntry method returns the IPv4 addresses of the local host
// alternatively, could use: Dns.GetHostEntry( Dns.GetHostName() )
IPHostEntry entries = Dns.GetHostEntry( string.Empty );
// find the local ipv4 address
IPAddress hostIp = entries.AddressList
                  .Single( x => x.AddressFamily == AddressFamily.InterNetwork );

一旦您获得了主机 IP,您就可以通过修改任何八位字节来使用 IP 字节创建新的 IP 地址。在您的情况下,您希望修改最后一个八位字节:

// grab the bytes from the host IP
var bytes = hostIp.GetAddressBytes();
// set the 4th octect (change 10 to whatever the 4th octect should be)
bytes[3] = 10;
// create a new IP address
var newIp = new IPAddress( bytes );

当然,您可以更改任何八位字节。上面的示例仅适用于第 4 个八位字节。如果您想要第一个八位字节,则可以使用bytes[0] = 10

Assuming there is only one network adapter:

// When an empty string is passed as the host name, 
// GetHostEntry method returns the IPv4 addresses of the local host
// alternatively, could use: Dns.GetHostEntry( Dns.GetHostName() )
IPHostEntry entries = Dns.GetHostEntry( string.Empty );
// find the local ipv4 address
IPAddress hostIp = entries.AddressList
                  .Single( x => x.AddressFamily == AddressFamily.InterNetwork );

Once you have the host IP, you can then use the IP bytes to create a new IP address by modifying any of the octets. In your case, you're wanting the last octect to be modified:

// grab the bytes from the host IP
var bytes = hostIp.GetAddressBytes();
// set the 4th octect (change 10 to whatever the 4th octect should be)
bytes[3] = 10;
// create a new IP address
var newIp = new IPAddress( bytes );

Of course, you can change any of the octets. The above example is only for the 4th octet. If you wanted the first octet, you'd use bytes[0] = 10.

始于初秋 2024-12-13 19:02:10

这是一个相当脆弱的方法,它依赖于机器的字节顺序,显然还依赖于所提供的地址系列。

byte[] ipBytes = ip.GetAddressBytes();
while (ipBytes[0]++ < byte.MaxValue)
{
  var newIp = new IPAddress(ipBytes);
  Console.WriteLine("    {0}", ip);
}

Here's a rather brittle method that relies on the byte order of your machine and, obviously, the family of the provided address.

byte[] ipBytes = ip.GetAddressBytes();
while (ipBytes[0]++ < byte.MaxValue)
{
  var newIp = new IPAddress(ipBytes);
  Console.WriteLine("    {0}", ip);
}
仙气飘飘 2024-12-13 19:02:10

您可以使用 IPAddress 对象的 ToString() 方法将其转换为字符串。

您是否考虑过仅使用 System.Net.IPAddress 对象?

这是有关其 Parse 方法的文档,该方法接受一个字符串并尝试将其转换为 IPAddress 对象,因此您可以执行您想做的任何字符串魔术: http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx

或者,如果你愿意的话要了解如何将字符串转换为数字,请尝试数字数据类型的 TryParse 方法。也许 Int32.TryParse 会起作用。

You can convert it to a string by using the IPAddress object's ToString() method.

Have you considered just using the System.Net.IPAddress object?

Here is the document on its Parse method, which takes a string and attempts to convert it to an IPAddress object, so you could do whatever string magic you wanted to do: http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx

Or, if you want to know how to convert a string to a number, try the numeric data type's TryParse method. Perhaps Int32.TryParse would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文