Fast way to check if a server is accessible over the network in C#

前端 未结 4 1987
情深已故
情深已故 2020-12-19 03:39

I\'ve got a project where I\'m hitting a bunch of custom Windows Performance Counters on multiple servers and aggregating them into a database. If a server is down, I want

4条回答
  •  一向
    一向 (楼主)
    2020-12-19 03:56

    The only "quick" way I think to see if it's up without relying on ping would be to create a socket, and see if you can actually connect to the port of the service you're trying to reach.

    This would be the equivalent of telnet servername 135 to see if it's up.

    Specifically...

    1. Create a .NET TCP socket client (System.Net.Sockets.TcpClient)
    2. Call BeginConnect() as an asynchronous operation, to connect to the server in question on one of the RPC ports that your directory exists code would use anyway (TCP 135, 139, or 445).
    3. If you don't hear back from it within X milliseconds, call Close() to cancel the connection.

    Disclaimer: I have no idea what effect this would have on any threat/firewall protection that may see this type of Connect / Disconnect with no data sent activity as a threat.

提交回复
热议问题