C#检查远程服务器 [英] C# Check Remote Server
问题描述
谁能指教一下,检查的最佳方法(使用.net 3.5),如果远程服务器可用?
我在考虑使用以下code,但想知道是否有更好的方式存在,如果社会上有另一种选择。
TcpClient的客户端=新的TcpClient(要播,80);
如果(!client.Connected)
{
抛出新的异常(无法连接到MYSERVER在端口80);
}
client.Close();
您可以的平它
您可以下载从它的默认页面
您可以做一个<一个href="http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx">HEAD要求
如果这是你的网络上的本地IIS6服务器,并且您有一些管理的细节,你可以的连接到IIS 使用一些的DirectoryEntry code
一些在<一个答案href="http://stackoverflow.com/questions/136615/how-can-i-test-a-connection-to-a-server-with-c-given-the-servers-ip-address">136615也会有所帮助,特别是接受的答案,讨论插座
有关的打印服务器(或,具体,打印机),在code被K斯科特这里可能的帮助。它的乐趣code与反正:-)这code提到dns.resolve,这是过时,取而代之的的 Dns.GetHostEntry
我很担心的想法: - )
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 a better way exists if the community has another option.
TcpClient client = new TcpClient("MyServer", 80);
if (!client.Connected)
{
throw new Exception("Unable to connect to MyServer on Port 80");
}
client.Close();
You could ping it
You could download the default page from it
You could do a HEAD request
If it's a local IIS6 server on your network, and you have some admin details, you could connect to IIS using some DirectoryEntry code
Some of the answers on 136615 might help too, specifically the accepted answer that talks about sockets
For the print servers (or, specifically, the printers), the code by K Scott here might help. It's fun code to play with anyway :-) That code mentions dns.resolve, which is obsoleted and replaced by Dns.GetHostEntry
I'm about out of ideas :-)
这篇关于C#检查远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!