Here you can find the source of waitForServerDown(String host, int port, long timeout)
public static boolean waitForServerDown(String host, int port, long timeout)
//package com.java2s; //License from project: Apache License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; public class Main { public static boolean waitForServerDown(String host, int port, long timeout) { long start = System.currentTimeMillis(); while (true) { try { send4LetterWord(host, port, "stat"); } catch (IOException e) { return true; }// w ww . j av a 2 s .c o m if (System.currentTimeMillis() > start + timeout) { break; } try { Thread.sleep(250); } catch (InterruptedException e) { // ignore } } return false; } public static String send4LetterWord(String host, int port, String cmd) throws IOException { Socket sock = new Socket(host, port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write(cmd.getBytes()); outstream.flush(); // this replicates NC - close the output stream before reading sock.shutdownOutput(); reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); } finally { sock.close(); if (reader != null) { reader.close(); } } } }