VB.Net返回IPv4地址
如何在VB.Net中返回IPv4地址 例如192.168.1.5VB.Net返回IPv4地址,vb.net,networking,ip,ipv4,Vb.net,Networking,Ip,Ipv4,如何在VB.Net中返回IPv4地址 例如192.168.1.5 Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()) Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString() 编辑: 然后您可以使用查找IP家族类型 编辑:
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()
编辑:
然后您可以使用查找IP家族类型
编辑:
然后您可以使用查找IP家族类型。类似这样的内容
Public Function GetIpV4() As String
Dim myHost As String = Dns.GetHostName
Dim ipEntry As IPHostEntry = Dns.GetHostEntry(myHost)
Dim ip As String = ""
For Each tmpIpAddress As IPAddress In ipEntry.AddressList
If tmpIpAddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
Dim ipAddress As String = tmpIpAddress.ToString
ip = ipAddress
exit for
End If
Next
If ip = "" Then
Throw New Exception("No 10. IP found!")
End If
Return ip
End Function
像这样的
Public Function GetIpV4() As String
Dim myHost As String = Dns.GetHostName
Dim ipEntry As IPHostEntry = Dns.GetHostEntry(myHost)
Dim ip As String = ""
For Each tmpIpAddress As IPAddress In ipEntry.AddressList
If tmpIpAddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
Dim ipAddress As String = tmpIpAddress.ToString
ip = ipAddress
exit for
End If
Next
If ip = "" Then
Throw New Exception("No 10. IP found!")
End If
Return ip
End Function
我所能做的就是,只返回IPv4地址 仅使用数组函数和lambda表达式,非常简洁:
Public Function GetHostEntryIPv4(ByVal addr As String) As IPHostEntry
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(addr)
If Not IsNothing(ipHostInfo) Then
ipHostInfo.AddressList = Array.FindAll(ipHostInfo.AddressList, Function(n As IPAddress) n.AddressFamily = AddressFamily.InterNetwork)
End If
GetHostEntryIPv4 = ipHostInfo
End Function
我所能做的就是,只返回IPv4地址 仅使用数组函数和lambda表达式,非常简洁:
Public Function GetHostEntryIPv4(ByVal addr As String) As IPHostEntry
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(addr)
If Not IsNothing(ipHostInfo) Then
ipHostInfo.AddressList = Array.FindAll(ipHostInfo.AddressList, Function(n As IPAddress) n.AddressFamily = AddressFamily.InterNetwork)
End If
GetHostEntryIPv4 = ipHostInfo
End Function
如果存在,将返回IPv6(我想)如果存在,将返回IPv6(我想)您想要给定主机名的IP地址吗?或者传入请求的IP地址?或者本地计算机的IP地址(可能有多个地址)?是否需要给定主机名的IP地址?或者传入请求的IP地址?或者本地计算机的IP地址(可能有多个地址)?