Here you can find the source of isListening(String host, int port)
public static boolean isListening(String host, int port) throws UnknownHostException, IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2007 Red Hat, Inc. /*w w w . j a va2s . c o m*/ * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.io.IOException; import java.net.ConnectException; import java.net.Socket; import java.net.UnknownHostException; public class Main { public static boolean isListening(String host, int port) throws UnknownHostException, IOException { Socket socket = null; try { socket = new Socket(host, port); return socket.isConnected(); } catch (ConnectException e) { return false; } finally { if (socket != null) { socket.close(); } } } }