Java HTTP Port Find waitForServerDown(String host, int port, long timeout)

Here you can find the source of waitForServerDown(String host, int port, long timeout)

Description

wait For Server Down

License

Apache License

Declaration

public static boolean waitForServerDown(String host, int port, long timeout) 

Method Source Code


//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();
            }
        }
    }
}

Related

  1. serverListening(String hostName, int port)
  2. waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
  3. waitForPort(String host, int port)
  4. waitForResponseCode(int code, String name, String host, int port)
  5. waitForRespose(String name, String host, int port)
  6. waitForService(int port)
  7. writeContactFile(int port, String appName)