- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想知道是否有人可以帮助我。我对 C# 的了解不多,但对于我想做的事情来说它很容易。
我正在制作一个小应用程序,它将接收我网络上的主机名,然后返回完整的 ipaddress(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 );
}
最佳答案
假设只有一个网络适配器:
// 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
。
关于c# - 更改 gethostentry 返回的 IP 的最后八位字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7589772/
我最近从 .NET v3.5 切换到 v4.0 Client Profile,第一次运行 GetHostEntry() 时遇到问题。 tcpClient.SocketInfo.So
我一直试图找到使用 GetHostAddresses 或 GetHostEntry 的正确位置。我通过阅读这篇文章 (http://msdn.microsoft.com/en-us/library/m
我来了from powershell 到 Mono: thufir@dur:~/mono$ thufir@dur:~/mono$ ls hello.cs thufir@dur:~/mono$ thuf
我只是想知道是否会出现主机名可以成功解析但返回的 hostEntry.AddressList 为空的情况。 目前我正在做这样的事情: IPHostEntry hostEntry = Dns.GetHo
关于 MSDN它提到 GetHostByName 已过时。替换为 GetHostEntry。他们有什么区别? 最佳答案 看起来 GetHostEntry 做了更多的错误检查并且还支持 Network
在新的 Windows 2012 服务器上 Dns.GetHostEntry Method (IPAddress)返回本地指定的主机名,但不是 DNS 已知的 IP 地址名称。 IP 地址是新服务器的
想知道是否有人可以帮助我。我对 C# 的了解不多,但对于我想做的事情来说它很容易。 我正在制作一个小应用程序,它将接收我网络上的主机名,然后返回完整的 ipaddress(ipv4) ....从那里我
我在 C# 中有一个 Windows 窗体应用程序,我正在尝试获取列表中所有客户端的主机名。下面给出的是来自此链接的 ra00l 的代码示例:GetHostEntry is very slow (我有
我有使用 Dns.GetHostEntry(hostNameOrIp) 的代码,我想检查一次该函数返回真实值的场景但在某个时间(当我决定) 此函数抛出异常。 目前我在 visual studio 20
我在文档中找不到任何关于这实际上做了什么的适当描述。 它是否检查 A 记录或 CNAME 记录或两者是否存在? 我的理解是,在 .NET 4 中,如果主机不存在,则会抛出 SocketExceptio
这个问题可能突出了我对网络原理缺乏理解,而不是编程问题。 我正在使用以下命令查找主机名 Dns.GetHostEntry 这会返回一个 IPHostEntry 给我,它有一个 AddressList
在 .NET 中,您可以调用 Dns.GetHostEntry(hostname) 这使用网络设置中的 DNS 条目进行 DNS 查找,但我想指定 DNS 服务器 8.8.8.8 进行查找,而不更改
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
假设在同一个网络中有两台计算机,名为 com1 和 com2。 在 com1 上,如果我调用 Dns.GetHostEntry("com2") 果然,它只返回 1 个 IP 地址,例如 192.168
我可以使用 Dns.GetHostEntry("google.com") 获取 uri 的 IP 地址,如“google.com”,例如: var ip = Dns.GetHostEn
我更改了我的代码,因此它不再使用已弃用的: Dns.GetHostByAddress(ipaddress); 使用: Dns.GetHostEntry(ipaddress); 问题是我从 Dns.Ge
我试图通过传入 IP 地址来获取主机名。我使用以下代码。 System.Net.Dns.GetHostEntry("192.168.x.x").HostName 对于某些主机,上面的代码正确返回了主机
我这里有一些代码在 IPv4 机器上运行良好,但在我们的构建服务器(IPv6)上它失败了。简而言之: IPHostEntry ipHostEntry = Dns.GetHostEntry(string
这台机器的 FQDN: thufir@dur:~$ thufir@dur:~$ hostname --fqdn dur.bounceme.net thufir@dur:~$ 是的...正在工作 dir
这是通过 USB 连接到我的计算机的手持设备,使用 Compact Framework: 当我调用 Dns.GetHostEntry("") 或 Dns.GetHostEntry(IPAddress.
我是一名优秀的程序员,十分优秀!