VB.Net Tutorial/Socket Network/Dns — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:55, 26 мая 2010

Dns.GetHostName

Imports System.Net

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(Dns.GetHostName)
    End Sub
End Class
vbex

Get Host Name and IP address

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
Computer Name: vbex   IP Address: 192.168.1.101

Get Host name by address

Imports System.Net
Imports System.Net.Sockets

Public Class Tester
    Public Shared Sub Main
        Dim strHostName As String
        Try
            strHostName = Dns.GetHostByAddress("68.129.2.123").HostName
            Console.WriteLine(strHostName)
        Catch exfe As FormatException
            Console.WriteLine(exfe.Message)
        Catch exse As SocketException
            Console.WriteLine(exse.Message)
        End Try
    End Sub
End Class
The requested name is valid and was found in the database, but it does not have the correct associat
ed data being resolved for

Get IP address by domain name

Imports System.Net
Imports System.Net.Sockets

Public Class Tester
    Public Shared Sub Main

        Try
            Dim myIPHostEntry As IPHostEntry = Dns.Resolve("www.google.ru")
            Dim myIPAddresses() As IPAddress = myIPHostEntry.AddressList
            Dim myIPAddress As IPAddress
            Dim strIPAddressList As String
            For Each myIPAddress In myIPAddresses
                Console.WriteLine(myIPAddress.ToString)
            Next
        Catch ex As SocketException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class
72.14.253.147
72.14.253.103
72.14.253.99
72.14.253.104

Resolve domain in a thread

Imports System.Net
Imports System.Net.Sockets

Public Class Tester
    Public Shared Sub Main
        Dim myThread As Threading.Thread
        myThread = New Threading.Thread(AddressOf BeginResolveIP)
        myThread.Start()
    End Sub
    Private Shared Sub BeginResolveIP()
        Try
            Dim myAsyncCallback As New AsyncCallback(AddressOf GetIPList)

            Dim myIAsyncResult As IAsyncResult = Dns.BeginResolve("www.google.ru", myAsyncCallback, Nothing)
            Do While Not myIAsyncResult.IsCompleted
                Threading.Thread.Sleep(10)
            Loop
            Threading.Thread.Sleep(2000)
        Catch ex As SocketException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    Private Shared Sub GetIPList(ByVal pIAsyncResult As IAsyncResult)
        Dim myIPHostEntry As IPHostEntry
        myIPHostEntry = Dns.EndResolve(pIAsyncResult)
        Dim myIPAddresses() As IPAddress = myIPHostEntry.AddressList
        Dim myIPAddress As IPAddress
        For Each myIPAddress In myIPAddresses
            Console.WriteLine(myIPAddress.ToString)
        Next
    End Sub
    
End Class
72.14.253.147
72.14.253.103
72.14.253.99
72.14.253.104