3

I have this code:

localIp = Request.UserHostName
hostName = DetermineCompName(localIp)

Session.Add("localIp", localIp)
Session.Add("hostName", hostName)

As you can see, I put the 2 variables on a session so that I can use it when I want. Testing the app on 10 computers, I saw that on some of the computers it gets the Client IP and the Computer name, but on others it goes empty.

As in some computers it works, I don't understand what's wrong. Does anyone have the right method to do this?

1
  • for hostname of the machine i've used: System.Net.Dns.GetHostEntry(Request.ServerVariables("remote_addr")).HostName assuming this is ASP ? For IP i think - Dim h As String = System.Net.Dns.GetHostEntry(Request.ServerVariables("remote_addr")).AddressList(0) ? Commented Jan 5, 2017 at 13:55

1 Answer 1

3

To get the computer name you can simply do:

Dim hostName As String = Environment.MachineName 

or:

Dim hostName As String = My.Computer.Name

For the IP it's a little bit trickier, I assume you want the ipV4, so you can try this:

Dim localIp As String
For Each address As System.Net.IPAddress In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList
    If address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
        localIp = address.ToString()
        Exit For
    End If
Next

Please note that if you do just:

System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()

then this will return the ipV6.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.