更改 gethostentry 返回的 IP 的最后一个八位字节
想知道是否有人可以帮助我。我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设只有一个网络适配器:
一旦您获得了主机 IP,您就可以通过修改任何八位字节来使用 IP 字节创建新的 IP 地址。在您的情况下,您希望修改最后一个八位字节:
当然,您可以更改任何八位字节。上面的示例仅适用于第 4 个八位字节。如果您想要第一个八位字节,则可以使用
bytes[0] = 10
。Assuming there is only one network adapter:
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:
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
.这是一个相当脆弱的方法,它依赖于机器的字节顺序,显然还依赖于所提供的地址系列。
Here's a rather brittle method that relies on the byte order of your machine and, obviously, the family of the provided address.
您可以使用 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.