Reputation: 69
How can I return the IPv4 address in VB.Net?
eg. 192.168.1.5
Upvotes: 1
Views: 12188
Reputation: 11
Best I can do is that, will return only IPv4 address just using array functions and lambda expressions, very clean :
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
Upvotes: 1
Reputation: 5570
Something like this
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
Upvotes: 4
Reputation: 2886
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()
edit:
then you can use IPAddress.AddressFamily to find out IP familly type.
Upvotes: 0