Java HTTP Port Find waitForPort(String host, int port)

Here you can find the source of waitForPort(String host, int port)

Description

wait For Port

License

Open Source License

Declaration

public static boolean waitForPort(String host, int port) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.Socket;

public class Main {
    private static final int RETRIES = 10;
    private static final int WAIT_TIME_MS = 2000;

    public static boolean waitForPort(String host, int port) {
        for (int i = 0; i < RETRIES; i++) {
            if (isPortAvailable(host, port))
                return true;

            try {
                Thread.sleep(WAIT_TIME_MS);
            } catch (InterruptedException e) {
                // no-op
            }/*  ww  w.  j  av  a  2  s. c o  m*/
        }
        return false;
    }

    public static boolean isPortAvailable(String host, int port) {
        Socket socket = null;
        boolean available = false;
        try {
            socket = new Socket(host, port);
            available = true;
        } catch (IOException e) {
            // no-op
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // no-op
                }
            }
        }
        return available;
    }
}

Related

  1. serverIsUp(int port)
  2. ServerListening(String host, int port)
  3. serverListening(String host, int port)
  4. serverListening(String hostName, int port)
  5. waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
  6. waitForResponseCode(int code, String name, String host, int port)
  7. waitForRespose(String name, String host, int port)
  8. waitForServerDown(String host, int port, long timeout)
  9. waitForService(int port)