C# Check Remote Server

后端 未结 5 1855
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 06:18

Can anyone advise what the best way to check (using .NET 3.5) if a remote server is available?

I was thinking of using the following code but would like to know if

5条回答
  •  青春惊慌失措
    2020-12-15 07:01

    If you just want to see whether a given server is online, then a simple ping should do the job in most cases.

    PingReply pingReply;
    using (var ping = new Ping())
        pingReply = ping.Send("http://www.stackoverflow.com/");
    var available = pingReply.Status == IPStatus.Success;
    

    Using this method you're not abusing the HTTP server in any way, too.

    Otherwise (if you want to check whether a connection is possible on a specific port), that basically looks fine.

提交回复
热议问题