VB.Net Tutorial/Socket Network/IP Address

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

Check equality for IP Address

Imports System.Net
Public Class Tester
    Public Shared Sub Main
        Dim ipAddr1 As IPAddress = IPAddress.Parse("192.168.1.100")
        Dim ipAddr2 As IPAddress = IPAddress.Parse("192.168.1.100")
    
        If ipAddr1.Equals(ipAddr2) Then
          Console.WriteLine("equal")
        Else
          Console.WriteLine("different")
        End If
    End Sub
End Class
equal

Get Host name by IP address

Imports System
Imports System.Data
Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
    
        Dim CpIP As IPHostEntry
        CpIP = Dns.GetHostByAddress("68.234.12.1")
        Console.WriteLine(CpIP.HostName)
    End Sub
End Class

Get IP address from IPEndPoint

Imports System.Net
Public Class Tester
    Public Shared Sub Main
        Dim ipAddr As System.Net.IPAddress
        Dim ipEndPoint As System.Net.IPEndPoint
        Dim socketAddr As System.Net.SocketAddress
    
        Try
          ipAddr = IPAddress.Parse("127.0.0.1")
          ipEndPoint = New System.Net.IPEndPoint(ipAddr, 12345)
    
          socketAddr = ipEndPoint.Serialize()
    
          Console.WriteLine("Address Family: " & ipEndPoint.AddressFamily.ToString)
          Console.WriteLine("IP:Port: " & ipEndPoint.Address.ToString & ":" & ipEndPoint.Port.ToString)
          Console.WriteLine("SocketAddress : " & socketAddr.ToString())
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString)
        End Try
    End Sub
End Class
Address Family: InterNetwork
IP:Port: 127.0.0.1:12345
SocketAddress : InterNetwork:16:{48,57,127,0,0,1,0,0,0,0,0,0,0,0}

Parse IP Address from string

Imports System.Net
Public Class Tester
    Public Shared Sub Main
        Dim ipAddr1 As IPAddress = IPAddress.Parse("192.168.1.100")
        Dim ipAddr2 As IPAddress = IPAddress.Parse("192.168.1.100")
    
        If ipAddr1.Equals(ipAddr2) Then
          Console.WriteLine("equal")
        Else
          Console.WriteLine("different")
        End If
    End Sub
End Class
equal