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.