-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingTask.java
45 lines (38 loc) · 938 Bytes
/
PingTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.net.*;
class PingTask implements Runnable {
private int timeout;
private Target target;
PingTask(Target target, int timeout) {
this.target = target;
this.timeout = timeout;
}
public String getHost() {
return target.getAddress().getAddress() + ":" + target.getAddress().getPort();
}
@Override
public void run() {
Socket connection = new Socket();
boolean reachable;
long start = System.currentTimeMillis();
try {
try {
connection.connect(target.getAddress(), timeout);
} finally {
connection.close();
}
long dur = (System.currentTimeMillis() - start);
System.out.printf("%5d %5d ms %s%n", target.getOrder(), dur, target.getAddress().getHostString() );
reachable = true;
} catch (Exception e) {
reachable = false;
}
if (!reachable) {
System.out.println(
String.format(
"\t%s was UNREACHABLE",
getHost()
)
);
}
}
} // PingTask class end.