Here you can find the source of isServing(String host, int port)
Parameter | Description |
---|---|
host | the host to try to connect to |
port | the port to try to connect on |
public static boolean isServing(String host, int port)
//package com.java2s; /*//w w w . j a v a 2 s .co m * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 * (the ?License??). You may not use this work except in compliance with the License, which is * available at www.apache.org/licenses/LICENSE-2.0 * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied, as more fully set forth in the License. * * See the NOTICE file distributed with this work for information regarding copyright ownership. */ import java.io.IOException; import java.net.Socket; public class Main { /** * @param host the host to try to connect to * @param port the port to try to connect on * @return whether a socket connection can be made to the given host on the given port */ public static boolean isServing(String host, int port) { if (port < 0) { return false; } try { Socket socket = new Socket(host, port); socket.close(); return true; } catch (IOException e) { return false; } } }