How to get server or localhost address in Win32 application.

Posted on 2004-11-22
Last Modified: 2008-01-09

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

Thanks !
Question by:dynamicrevolutions

    Accepted Solution


    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
                '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
            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("")
        End Sub

    I hope this helped.

    LVL 4

    Expert Comment

    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()

    LVL 11

    Expert Comment


    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

    LVL 11

    Expert Comment

    oops.. sorry guys.. i din't notice your posts..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now