Here you can find the source of isLocalPortUsed(int port)
Parameter | Description |
---|---|
port | a parameter |
public static boolean isLocalPortUsed(int port)
//package com.java2s; //License from project: Apache License import java.io.*; import java.net.InetAddress; import java.net.Socket; public class Main { /**// w w w. java 2 s. c o m * Check if aport is being used in the local host * * @param port * @return */ public static boolean isLocalPortUsed(int port) { boolean flag = true; try { flag = isPortUsing("127.0.0.1", port); } catch (Exception e) { } return flag; } /** * Check if a port is being used in the specified host * * @param host * @param port * @return */ public static boolean isPortUsing(String host, int port) { boolean flag = false; try { InetAddress theAddress = InetAddress.getByName(host); Socket socket = new Socket(theAddress, port); flag = true; } catch (IOException e) { //do nothing } return flag; } }