public PingResult ping(ScanningSubject subject, int count) throws IOException { PingResult result = new PingResult(subject.getAddress()); DatagramSocket socket = sockets.bind(new DatagramSocket()); socket.setSoTimeout(timeout); socket.connect(subject.getAddress(), PROBE_UDP_PORT); for (int i = 0; i < count && !Thread.currentThread().isInterrupted(); i++) { long startTime = System.currentTimeMillis(); byte[] payload = new byte[8]; ByteBuffer.wrap(payload).putLong(startTime); DatagramPacket packet = new DatagramPacket(payload, payload.length); try { socket.send(packet); socket.receive(packet); } catch (PortUnreachableException e) { result.addReply(System.currentTimeMillis() - startTime); } catch (SocketTimeoutException ignore) { } catch (NoRouteToHostException e) { // this means that the host is down break; } catch (SocketException e) { if (e.getMessage().contains(/*No*/ "route to host")) { // sometimes 'No route to host' also gets here... break; } } catch (IOException e) { LOG.log(FINER, subject.toString(), e); } } return result; }
public void close() { sockets.close(); }