locked
How to determine if a server is on a network RRS feed

  • Question

  • In the application, the end users needs to enter the name of data server.  I want to discover if this server exists on a network.

    Any help would be appreciated.


    ISV using VB.net and SQL Server

    Monday, July 2, 2018 9:58 PM

Answers

  • Hi Jeff

    Can your question be understood as whether you can connect to a server? If so, I hope the following code can help you.

    Imports System.Net
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MessageBox.Show(IsCanConnect("https://translate.google.cn"))
        End Sub
        Private Shared Function IsCanConnect(ByVal url As String) As Boolean
            Dim req As HttpWebRequest = Nothing
            Dim res As HttpWebResponse = Nothing
            Dim CanCn As Boolean = True
    
            Try
                req = CType(WebRequest.Create(url), HttpWebRequest)
                res = CType(req.GetResponse(), HttpWebResponse)
            Catch __unusedException1__ As Exception
                CanCn = False
            Finally
    
                If res IsNot Nothing Then
                    res.Close()
                End If
            End Try
    
            Return CanCn
        End Function
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff07 Thursday, July 5, 2018 3:14 PM
    Tuesday, July 3, 2018 5:05 AM

All replies

  • Are you asking if a specific SQL-Server name is currently available on a network drive e.g. if the SQL-Server instance name is MyCompanyServer1 etc ?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 2, 2018 10:31 PM
  • Not a SQL Server, but a regular Windows 2016 server.


    ISV using VB.net and SQL Server

    Monday, July 2, 2018 10:49 PM
  • Hi Jeff

    Can your question be understood as whether you can connect to a server? If so, I hope the following code can help you.

    Imports System.Net
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MessageBox.Show(IsCanConnect("https://translate.google.cn"))
        End Sub
        Private Shared Function IsCanConnect(ByVal url As String) As Boolean
            Dim req As HttpWebRequest = Nothing
            Dim res As HttpWebResponse = Nothing
            Dim CanCn As Boolean = True
    
            Try
                req = CType(WebRequest.Create(url), HttpWebRequest)
                res = CType(req.GetResponse(), HttpWebResponse)
            Catch __unusedException1__ As Exception
                CanCn = False
            Finally
    
                If res IsNot Nothing Then
                    res.Close()
                End If
            End Try
    
            Return CanCn
        End Function
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff07 Thursday, July 5, 2018 3:14 PM
    Tuesday, July 3, 2018 5:05 AM
  • Not a SQL Server, but a regular Windows 2016 server.


    You can use WNetGetResourceInformation

    <DllImport("Mpr.dll", SetLastError:=True, CharSet:=CharSet.Unicode, EntryPoint:="WNetGetResourceInformationW")>
    Public Shared Function WNetGetResourceInformation(ByRef lpNetResource As NETRESOURCE, lpBuffer As IntPtr, ByRef lpcbBuffer As Integer, ByRef lplpSystem As String) As Integer
    End Function
    
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure


    Tuesday, July 3, 2018 9:46 AM
  • In the application, the end users needs to enter the name of data server.  I want to discover if this server exists on a network.

    Any help would be appreciated.


    ISV using VB.net and SQL Server

    Please clarify:

    Do you want the user to enter a server name and then verify that the server is available?
    -OR-
    Do you want the user to press a button and have the software find the server name without any user input?

    The former could be achieved with a DNS lookup and Ping.

    The latter would require that the server be running some service which is discoverable.  If the server is an Active Directory primary domain controller then you could query the network for the PDC.  Otherwise you would need to create some kind of discovery service of your own to run on the server, or use some existing software which may already be installed that provides a discovery service.  We would need to know more about the server's role on the network.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 3, 2018 10:41 AM
  • The first scenario applies to my case.  Don't need to determine if the user can login...just that the server exists.  See example below.


    ISV using VB.net and SQL Server

    Tuesday, July 3, 2018 4:48 PM
  • The first scenario applies to my case.  Don't need to determine if the user can login...just that the server exists.  See example below.


    ISV using VB.net and SQL Server


    Then WNetGetResourceInformation as mentioned does that for a Windows server
    Tuesday, July 3, 2018 5:03 PM
  • The first scenario applies to my case.  Don't need to determine if the user can login...just that the server exists.  See example below.


    ISV using VB.net and SQL Server


    Then rather than dip into unmanaged code, I would probably just use Dns.GetHostEntry to find the server in DNS and then Ping to see if it is responding.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, July 3, 2018 7:13 PM
  • Tuesday, July 3, 2018 7:24 PM
  • Then rather than dip into unmanaged code, I would probably just use Dns.GetHostEntry to find the server in DNS and then Ping to see if it is responding.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Ping is usually disabled on servers : Why can't I ping my Windows server?

    Tuesday, July 3, 2018 8:08 PM
  • An internal network server usually isn't running default firewall configs... but I suppose that is a good point.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 3, 2018 8:27 PM