Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.hydrotekz.NetMonitor.pingers.MC;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.nio.channels.SocketChannel;
- public class Test {
- public static long getResponseTime(String hostAddress, int port) {
- InetAddress inetAddress = null;
- InetSocketAddress socketAddress = null;
- SocketChannel sc = null;
- long timeToRespond = -1;
- long start, stop;
- try {
- inetAddress = InetAddress.getByName(hostAddress);
- socketAddress = new InetSocketAddress(inetAddress, port);
- // Open the channel, set it to non-blocking, initiate connect
- sc = SocketChannel.open();
- sc.configureBlocking(true);
- start = System.nanoTime();
- if (sc.connect(socketAddress)) {
- stop = System.nanoTime();
- timeToRespond = (stop - start)/1000000;
- }
- } catch (Exception e){
- // Quiet error
- } finally {
- try {
- if (sc != null) sc.close();
- } catch (Exception e) {
- // Quiet error
- }
- }
- return timeToRespond;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment