Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether file share server is online? [duplicate]

Tags:

.net

vb.net

If I want to check server availability but I don't know about its shares, I can type its UNC name (or IP address) into Windows Explorer window even without server share directory:

Explorer screenshot from Windows 8

How I can use this programatically in .NET to verify whether server is online? I'm trying

My.Computer.FileSystem.DirectoryExists(UNCPath)

but this works only for UNC paths like \\MY_SERVER\Users, it is not inteded to use with \\MY_SERVER.

I would like to avoid Ping and stick with functionality which works when file system works because some servers can have ICMP_ECHO disabled due to security reasons.

(In case if you wish to post some code, feel free to choose VB or C#.)

like image 992
miroxlav Avatar asked Nov 18 '25 08:11

miroxlav


1 Answers

Edit: I'm notified this may be duplicate solution to Q/A mentioned elsewhere. The difference is that the other question accepts the answer which did not really work for me and I have reasons to think that using WMI is limiting approach in this scenario (it requires credentials, see below). In this answer, I'm giving an approach which really works for me. But if you really think it is duplicate, continue marking it for closing and I'll remove the entire question as unnecessary.


I've continued studying and trying and finally I settled with using TCP port 139, the same which is used by file sharing mechanism. Consider the following source as a draft (and do not forget to handle exceptions etc.), but finally I can have piece of code which seems to work for me:

Public Shared Function IsFileShareServerOnline(PCName As String,
                                               Timeout as Integer) As Boolean

    Const Port As Integer = 139 'port for file sharing service

    Dim IsOnline As Boolean = False
    Dim IP As Net.IPAddress = Net.Dns.GetHostAddresses(PCName)(0)
    Dim Address As Net.IPAddress = Net.IPAddress.Parse(IP.ToString())
    Dim Socket As Net.Sockets.TcpClient = New Net.Sockets.TcpClient(Address.AddressFamily)
    Dim Connect = Socket.BeginConnect(Address, Port, Nothing, Nothing)
    Dim WaitingResult = Connect.AsyncWaitHandle.WaitOne(Timeout, False)

    If (Connect.IsCompleted) Then
        If WaitingResult Then
            Socket.EndConnect(Connect)
            IsOnline = True
        End If
    End If
    Socket.Close()
    Return IsOnline

End Function

The code has an issue that on first run it cannot detect remote computer even if available. Upon retry it works. The problem renews (one bad detection) whenever the connection is attempted after longer time. I'm not TCP/IP expert to understand the cause, maybe I use too short timeout (1 sec). A workaround is to give at least 2 connection attempts.

Before trying this approach, I've also implemented and tested WMI-based queries, but they did not work without prior authentication at remote computer what was pretty useless in my scenario.

like image 77
miroxlav Avatar answered Nov 21 '25 04:11

miroxlav