Here you can find the source of isPortAvailable(String portNumber)
Parameter | Description |
---|---|
portNumber | to check (as a String) |
Parameter | Description |
---|---|
NumberFormatException | an exception |
public static boolean isPortAvailable(String portNumber) throws NumberFormatException
//package com.java2s; /******************************************************************************* * Copyright 2012 Google Inc. All Rights Reserved. * /*from www . j a v a 2 s .c o m*/ * All rights reserved. This program and the accompanying materials * are 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 * * 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.ServerSocket; public class Main { /** * Function to check if a port is available * * @param portNumber to check (as a String) * @return true if port is available, false otherwise * @throws NumberFormatException */ public static boolean isPortAvailable(String portNumber) throws NumberFormatException { final int port = Integer.parseInt(portNumber); ServerSocket ss = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); return true; } catch (IOException e) { // } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { // probably shouldn't get here } } } return false; } }