Edit: I am notified that this may be a duplicate solution for Q / A mentioned elsewhere . The difference is that another question accepts an answer that actually did not work for me, and I have reason to believe that using WMI limits the approach in this scenario (this requires credentials, see below). In this answer, I give an approach that really works for me. But if you really think this is a duplicate, keep marking it for closing and I will delete the whole question as unnecessary.
I continued to study and try, and finally I decided to use TCP port 139 , the same as that used by the file sharing mechanism . Consider the following source as a draft (and don't forget to handle exceptions, etc.), but finally, I can have a piece of code that 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
There is a problem in the code that, at the first start, the remote computer cannot be detected, even if it is available. After repeating it works. The problem resumes (one bad detection) whenever a connection is made after a longer time. I am not a TCP / IP expert to understand the reason, maybe I am using a too short timeout (1 sec). The workaround is to give at least 2 connection attempts.
Before trying this approach, I also implemented and tested WMI-based requests, but they did not work without preliminary authentication on the remote computer, which was pretty useless in my scenario.
miroxlav
source share