How to get server or localhost address in VB.net Win32 application.

Hi..

How do i get a server IP address in VB.net win32 application.

Thanks !
LVL 5
dynamicrevolutionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pfranssonCommented:
Hi,

Is this what you are looking for?

Imports System.Net
Public Class cslGetServer


    Shared Function returnAddressOnNetwork(ByVal hn As String) As String
        Dim ia As String
        Try
            'Returns the IP address of the host computer as a string.
'actually only the first IP address. Access array if the server has more than one interface.
            ia = Dns.GetHostByName(hn).AddressList(0).ToString
            Return ia
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Function



End Class

How to call:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'call using namespace.classname.function name passing hostname to function
        Dim str As String = ipResolver.cslGetServer.returnAddressOnNetwork("mail.webhost4life.com")
        MessageBox.Show(Str)
    End Sub

I hope this helped.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhprCommented:
Hi dynamicrevolutions,

try this...

Import System.Net

 Dim ipEntry As IPHostEntry = Dns.GetHostByName(Environment.MachineName)
 Dim IpAddr As IPAddress() = ipEntry.AddressList
 Dim i As Integer

 'A hostmachine can have more than one IP assigned

  Me.Label2.Text = IpAddr(0).ToString()

-bhpr
0
pratap_rCommented:
hey..

check if this is what you are looking for..

        Dim ip As System.Net.IPHostEntry
        ip = System.Net.Dns.GetHostByName("<server name>")
        MsgBox(ip.AddressList(0).ToString) 'will display the first IP assigned


you might hav eto add error checks

Enjoy!
Pratap
0
pratap_rCommented:
oops.. sorry guys.. i din't notice your posts..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.