Link to home
Start Free TrialLog in
Avatar of suvmitra
suvmitraFlag for India

asked on

Return IPv4 address in VB.NET 2008

The below code snippet is working perfectly ASP.net pages.

I need to return  IPv4 address of the client in a vb.net windows application form and need to change this code to work.

HttpContext.Current.Request.UserHostAddress is not working in windows forms ..pls help.
Imports System
Imports System.Net

Public Class IPNetworking
  Public Shared Function GetIP4Address() As String
    Dim IP4Address As String = String.Empty

    For Each IPA As IPAddress In Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress)
      If IPA.AddressFamily.ToString() = "InterNetwork" Then
        IP4Address = IPA.ToString()
        Exit For
      End If
    Next

    If IP4Address <> String.Empty Then
      Return IP4Address
    End If

    For Each IPA As IPAddress In Dns.GetHostAddresses(Dns.GetHostName())
      If IPA.AddressFamily.ToString() = "InterNetwork" Then
        IP4Address = IPA.ToString()
        Exit For
      End If
    Next

    Return IP4Address
  End Function
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abhinayp
abhinayp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial