Here you can find the source of isServerAlive(InetAddress host, int port)
Parameter | Description |
---|---|
host | the server host |
port | the server port |
public static boolean isServerAlive(InetAddress host, int port)
//package com.java2s; /*/*from w w w. j a v a 2 s. co m*/ * Copyright [2013-2014] eBay Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.net.InetAddress; import java.net.Socket; public class Main { /** * Client connection retry count */ public static final int RETRY_COUNT = 4; /** * Check whether a server is alive. * * @param host * the server host * @param port * the server port * @return true if a server is alive, false if a server is not alive. */ public static boolean isServerAlive(InetAddress host, int port) { Socket socket = null; int i = 0; while (i < RETRY_COUNT) { try { socket = new Socket(host, port); break; } catch (IOException e) { i++; try { Thread.sleep(1000); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } } finally { try { if (socket != null) { socket.close(); } } catch (IOException ignore) { } } } return i != RETRY_COUNT; } /** * Check whether a server is alive. * * @param host * the server host * @param port * the server port * @return true if a server is alive, false if a server is not alive. */ public static boolean isServerAlive(String host, int port) { Socket socket = null; int i = 0; while (i++ < RETRY_COUNT) { try { socket = new Socket(host, port); break; } catch (IOException e) { try { Thread.sleep(1000); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } } finally { try { if (socket != null) { socket.close(); } } catch (IOException ignore) { } } } return i < RETRY_COUNT; } }