TheCreepySheep
TheCreepySheep

Reputation: 125

IfElse checking if scan equals to IPv4

I would like to know, how to make If-statement check, if myClientMachineIp (from the code) equals to AddressFamily.InterNetwork?

My current code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
        Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()

        If myClientMachineIP = AddressFamily.InterNetwork Then
            TextBox1.Text = myClientMachineIP
        Else
            TextBox1.Text = "IP does not equal to IPv4"

        End If
    End Sub

Upvotes: 0

Views: 74

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32627

Don't throw away valuable information by calling ToString(). Use the IPAddress type:

Dim myClientMachineAddressList _
    = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP = myClientMachineAddressList.AddressList(0)

If myClientMachineIP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
    TextBox1.Text = myClientMachineIP.ToString()
Else
    TextBox1.Text = "IP does not equal to IPv4".
End If

Upvotes: 1

Related Questions