8

How to get ip address in vb.net. i used below code to get local ip address but it showing dns is not declared. can any one tell me what is that Dns in the Code

VB Code

Imports System.Environment
Imports System.Net

Public Class Tester
Public Shared Sub Main
Dim hostname As String = Dns.GetHostName()
Dim ipaddress As String = CType(Dns.GetHostByName(hostname).AddressList.GetValue(0), IPAddr
ess).ToString
Console.WriteLine("Computer Name: " & hostname & " IP Address: " & ipaddress)
End Sub

End Class
Yahia
  • 69,653
  • 9
  • 115
  • 144
Rajkumar Reddy
  • 2,357
  • 6
  • 24
  • 29
  • You know that a machine may have none or several ip addresses? – Joel Coehoorn Aug 27 '11 at 07:21
  • 2
    now you have changed the question completely - first you asking about `Dns`, now you are asking about iüp of connected client... IF you have a second question you must open a second question and NOT change the first one !!! I rolled back your question to the original one... please upvote/mark as accepted if any answer was of help AND open a second question about "client ip"! – Yahia Aug 27 '11 at 13:05
  • 2
    obviously someone that does not know to ask a question in a polite, acceptable, civil, human, normal, decent way – Martin Aug 27 '11 at 14:36
  • use dns Class , more information here http://msdn.microsoft.com/it-it/library/system.net.dns.aspx Regards. – Carmelo La Monica Aug 27 '11 at 13:14

8 Answers8

6

Since I get the feeling that, the question (in the title) is not fully answered yet ...

Dim hostName = System.Net.Dns.GetHostName()
For Each hostAdr In System.Net.Dns.GetHostEntry(hostName).AddressList()

    ' If you just want to write every IP
    Console.WriteLine("Name: " & hostName & " IP Address: " & hostAdr.ToString() 

    ' If you want to look if the device is member of a specific network
    If hostAdr.ToString().StartsWith("192.168.1.") Then DoSomething() : Exit For

    ' I think you get the idea ^^
    ' ...
Next

... obviously this is not exactly what the OP asked for, but just from the title and google links, this should answer what people coming here are looking for.

Btw GetHostByName()seems to be deprecated, GetHostEntry() like this works the same way, without throwing a warning.

Levite
  • 17,263
  • 8
  • 50
  • 50
2

Dns is a class in the namespace System.Net which provides functionality regarding the "Domain Name System" (thus the name Dns) - see http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx

Yahia
  • 69,653
  • 9
  • 115
  • 144
1
Dim hostName = System.Net.Dns.GetHostName()
    For Each hostAdr In System.Net.Dns.GetHostEntry(hostName).AddressList()

        ' If you just want to write every IP
        'Console.WriteLine("Name: " & hostName & " IP Address: " & hostAdr.ToString())
        Me.RichTextBox1.Text = hostAdr.ToString

        'If you want to look if the device is member of a specific network

        ' ...
    Next
bibi
  • 3,671
  • 5
  • 34
  • 50
Deepesh
  • 11
  • 1
1

Use this:

HttpContext.Current.Request.UserHostAddres

Hope this helps.

talha2k
  • 24,937
  • 4
  • 62
  • 81
0

I found here a good example to get the own IP.

        Dim _IP As String = Nothing

        Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())

        For Each _IPAddress As System.Net.IPAddress In _IPHostEntry.AddressList
            If _IPAddress.AddressFamily.ToString() = "InterNetwork" Then
                _IP = _IPAddress.ToString()
            End If
        Next _IPAddress
        Return _IP
Andreas
  • 1
  • 2
0

ips = Dns.GetHostAddresses(hostname)

here is a sample codeMSDN

Arjun Shetty
  • 1,575
  • 1
  • 15
  • 36
-1
Dim ipaddress As String =
    Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString

This will most likely give IPv6 address

And

Dim ipaddress As String =
    Dns.GetHostEntry(Dns.GetHostName).AddressList.FirstOrDefault(() => { },
    (ip.AddressFamily = AddressFamily.InterNetwork)).ToString

This will give IPv4 address

My VB is rusty so here is a C# code that works for me.

using System.Linq;

using System.Net.Sockets;

string IPaddress =
    Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString(); //For Ipv6

string IPaddress =
    Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip =>
    ip.AddressFamily == AddressFamily.InterNetwork).ToString(); //For Ipv4

I hope this helps.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
abhi
  • 1
  • 1
-1

You can get local ip address of system using below code:

Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
  • Really this is a bad and typicall answer. what happens when the IP that we desire is at index 0 or 2 of 'AddressList()'? – ElektroStudios May 01 '14 at 16:27
  • This would be a better solution, but still not be 100% efficient: Return (From IP As Net.IPAddress In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList() Where Not IP.ToString.Contains(":")).FirstOrDefault.ToString – ElektroStudios May 01 '14 at 16:35
  • What if a loopback adapter or Bluetooth LAN adapter was installed? You're assuming the network adapter 1 is the correct one without checking. The problem is - the index array starts at 0, not one. There may not even be an adapter 1 in the array. The best way is to foreach iterate through the adapters and [decide] which one is correct by simple means of validation to test for ipv4, ipv6 or bt addresses. –  Nov 28 '15 at 09:02