Here you can find the source of isLocalPortAvailable(final int port)
Parameter | Description |
---|---|
port | the port to check for availability |
public static boolean isLocalPortAvailable(final int port)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; public class Main { /**//from w w w .j ava2 s . c om * Checks to see if a specific <b>local</b> port is available. * * @param port the port to check for availability * @return {@code true} if port is available */ public static boolean isLocalPortAvailable(final int port) { try (final DatagramSocket ds = new DatagramSocket(port); final ServerSocket ss = new ServerSocket(port)) { ss.setReuseAddress(true); ds.setReuseAddress(true); return true; } catch (final IOException ignored) { return false; } } }