你可以使用ping从.NET,但它需要服务器的IP地址。
从here:
internal bool PingServer()
{
bool netOK = false;
// 164.110.12.144 is current server address for server: nwhqsesan02
byte[] AddrBytes = new byte[] { 164, 110, 12, 144 }; // byte array for server address.
using (System.Net.NetworkInformation.Ping png = new System.Net.NetworkInformation.Ping())
{
System.Net.IPAddress addr;
// Sending ping to a numeric byte address has the best change of
// never causing en exception, whether network connected or not.
addr = new System.Net.IPAddress(AddrBytes);
try
{
netOK = (png.Send(addr, 1500, new byte[] { 0, 1, 2, 3 }).Status == IPStatus.Success);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
netOK = false;
}
return netOK;
}
}
编辑:这个怎么样:
bool ConnectionExists()
{
try
{
System.Net.Sockets.TcpClient clnt=new System.Net.Sockets.TcpClient("www.google.com",80);
clnt.Close();
return true;
}
catch(System.Exception ex)
{
return false;
}
}
不起作用。 PING需要服务器允许ICMP--这是“我不能在客户站点执行的事情”(是的,我无法平衡我连接的一半服务 - 在金融行业愚蠢的疯狂思维)。 – TomTom 2010-04-15 16:13:32
金融业啊,它不疯狂,他们有我们所有的钱。检查我的更新,如果有效。 – 2010-04-15 16:46:47
这并不告诉我我的自己的软件服务器在哪里。这不是“查找我是否有互联网”,而是让我自己的软件在局域网场景中尝试猜测它自己的(安装的)服务器,以便人们不必输入服务器名称。与例如Exchange Auto-Config类似,或者与Office Communicator为自己查找OCS实例的方式类似。 – TomTom 2010-04-17 16:12:36