VB.Net Возврат IPv4-адреса - PullRequest
1 голос
/ 26 мая 2010

Как вернуть адрес IPv4 в VB.Net?

например. 192.168.1.5

Ответы [ 3 ]

4 голосов
/ 26 мая 2010

как то так

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
1 голос
/ 25 января 2011

Лучшее, что я могу сделать, это вернуть только адрес IPv4 просто используя функции массива и лямбда-выражения, очень чисто:

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
0 голосов
/ 26 мая 2010
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()

редактирование:

тогда вы можете использовать IPAddress.AddressFamily , чтобы узнать тип фамилии IP.

...