Java HTTP Port checkIfPortAvailable(int port)

Here you can find the source of checkIfPortAvailable(int port)

Description

check If Port Available

License

Apache License

Declaration

private static boolean checkIfPortAvailable(int port) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.net.DatagramSocket;

import java.net.ServerSocket;

public class Main {
    private static boolean checkIfPortAvailable(int port) {
        ServerSocket tcpSocket = null;
        DatagramSocket udpSocket = null;

        try {//from  w w w. ja  v a  2s. c o m
            tcpSocket = new ServerSocket(port);
            tcpSocket.setReuseAddress(true);

            udpSocket = new DatagramSocket(port);
            udpSocket.setReuseAddress(true);
            return true;
        } catch (IOException ex) {
            // denotes the port is in use
        } finally {
            if (tcpSocket != null) {
                try {
                    tcpSocket.close();
                } catch (IOException e) {
                    /* not to be thrown */
                }
            }

            if (udpSocket != null) {
                udpSocket.close();
            }
        }

        return false;
    }
}

Related

  1. checkHostAvailability(String host, int port)
  2. checkHostPort(String connectionString)
  3. create(final int[] ports)
  4. create(String scheme, String rawUserInfo, String host, int port, String rawPath, String rawQuery, String rawFragment)
  5. createProxy(final Proxy.Type type, final String host, final int port)
  6. createServer(final String host, final int port)