C#里面有Ping类,可以开个线程定时的去ping 服务器,看看是否能通
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class
PingSwitch
{
public
static
bool
DoPing(
string
ip)
{
Ping pingSender =
new
Ping();
PingOptions options =
new
PingOptions();
options.DontFragment =
true
;
string
data =
"a"
;
byte
[] buffer = Encoding.ASCII.GetBytes(data);
int
timeout = 120;
PingReply reply = pingSender.Send(ip, timeout, buffer, options);
return
reply.Status == IPStatus.Success;
}
}
|