Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

.net - C# Check Remote Server

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...