Example usage for java.net BindException BindException

List of usage examples for java.net BindException BindException

Introduction

In this page you can find the example usage for java.net BindException BindException.

Prototype

public BindException(String msg) 

Source Link

Document

Constructs a new BindException with the specified detail message as to why the bind error occurred.

Usage

From source file:Main.java

public static void checkIfValidAddress(InetAddress bind_addr, String prot_name) throws Exception {
    //if(bind_addr.isAnyLocalAddress() || bind_addr.isLoopbackAddress())
    //  return;/* w  w w . j a  v a2s .co m*/
    Collection<InetAddress> addrs = getAllAvailableAddresses();
    for (InetAddress addr : addrs) {
        if (addr.equals(bind_addr))
            return;
    }
    throw new BindException(
            "[" + prot_name + "] " + bind_addr + " is not a valid address on any local network interface");
}

From source file:it.jnrpe.server.CBindingThread.java

public CBindingThread(CBinding binding) throws IOException {
    m_Binding = binding;/*www .  ja  v  a2 s. co m*/
    try {
        init();
    } catch (Exception e) {
        throw new BindException(e.getMessage());
    }
}

From source file:com.chiralBehaviors.slp.hive.hardtack.configuration.AggregatorConfiguration.java

@Override
public Engine construct() throws IOException {
    NetworkInterface intf = getNetworkInterface();
    DatagramSocket socket;/*from  w w w.j a va2s .  c o  m*/
    InetSocketAddress address;
    if (endpoint.getAddress().isAnyLocalAddress()) {
        address = new InetSocketAddress(Utils.getAddress(intf, ipv4), endpoint.getPort());
    } else {
        address = endpoint;
    }
    try {
        socket = new DatagramSocket(address);
    } catch (BindException e) {
        BindException bindException = new BindException(String.format("Cannot bind to %s", address));
        bindException.initCause(e);
        throw bindException;
    }
    return new AggregatorEngine(socket, getMac(), Executors.newSingleThreadScheduledExecutor(), getFdFactory());
}

From source file:hudson.TcpSlaveAgentListener.java

/**
 * @param port// w  w  w.  j av  a2  s.c o m
 *      Use 0 to choose a random port.
 */
public TcpSlaveAgentListener(int port) throws IOException {
    super("TCP agent listener port=" + port);
    try {
        serverSocket = ServerSocketChannel.open();
        serverSocket.socket().bind(new InetSocketAddress(port));
    } catch (BindException e) {
        throw (BindException) new BindException(
                "Failed to listen on port " + port + " because it's already in use.").initCause(e);
    }
    this.configuredPort = port;
    setUncaughtExceptionHandler((t, e) -> {
        LOGGER.log(Level.SEVERE,
                "Uncaught exception in TcpSlaveAgentListener " + t + ", attempting to reschedule thread", e);
        shutdown();
        TcpSlaveAgentListenerRescheduler.schedule(t, e);
    });

    LOGGER.log(Level.FINE, "TCP agent listener started on port {0}", getPort());

    start();
}

From source file:com.taobao.adfs.distributed.rpc.Server.java

/**
 * A convenience method to bind to a given address and report better exceptions if the address is not a valid host.
 * /*from  w  w w.j a  va2  s  .  c o  m*/
 * @param socket
 *          the socket to bind
 * @param address
 *          the address to bind to
 * @param backlog
 *          the number of connections allowed in the queue
 * @throws BindException
 *           if the address can't be bound
 * @throws UnknownHostException
 *           if the address isn't a valid host name
 * @throws IOException
 *           other random errors from bind
 */
public static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        BindException bindException = new BindException(
                "Problem binding to " + address + " : " + e.getMessage());
        bindException.initCause(e);
        throw bindException;
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        } else {
            throw e;
        }
    }
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static ServerSocket createServerSocket(int startPort, int endPort) throws BindException {
    ServerSocket ssoc = null;//from ww w .  ja  v a2 s  .  c  om
    ServerSocketFactory factory = ServerSocketFactory.getDefault();

    int port = startPort;
    while (true) {
        try {
            ssoc = factory.createServerSocket(port);
            break;
        } catch (IOException e) {
            if (port >= endPort)
                throw new BindException(
                        "No available port to bind to in range [" + startPort + " .. " + endPort + "]");
            port++;
        }
    }

    return ssoc;
}

From source file:de.kapsi.net.daap.nio.DaapServerNIO.java

/**
 * Binds this server to the SocketAddress supplied by DaapConfig
 * /*w w w. j  a  v a  2s  . co  m*/
 * @throws IOException
 */
public void bind() throws IOException {

    SocketAddress bindAddr = config.getInetSocketAddress();
    int backlog = config.getBacklog();

    try {

        ssc = ServerSocketChannel.open();
        ServerSocket socket = ssc.socket();

        // BugID: 4546610
        // On Win2k, Mac OS X, XYZ it is possible to bind
        // the same address without rising a SocketException
        // (the Documentation lies)
        socket.setReuseAddress(false);

        try {
            socket.bind(bindAddr, backlog);
        } catch (SocketException err) {
            throw new BindException(err.getMessage());
        }

        ssc.configureBlocking(false);

        if (LOG.isInfoEnabled()) {
            LOG.info("DaapServerNIO bound to " + bindAddr);
        }

        streams = new HashSet();
        connections = new HashSet();
        sessionIds = new HashSet();

    } catch (IOException err) {
        close();
        throw err;
    }
}

From source file:com.hortonworks.hbase.replication.bridge.HBaseServer.java

/**
 * A convenience method to bind to a given address and report
 * better exceptions if the address is not a valid host.
 * @param socket the socket to bind/*from  www .j av  a 2 s. co  m*/
 * @param address the address to bind to
 * @param backlog the number of connections allowed in the queue
 * @throws BindException if the address can't be bound
 * @throws UnknownHostException if the address isn't a valid host name
 * @throws IOException other random errors from bind
 */
public static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        BindException bindException = new BindException(
                "Problem binding to " + address + " : " + e.getMessage());
        bindException.initCause(e);
        throw bindException;
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        }
        throw e;
    }
}

From source file:org.metaeffekt.dcc.agent.DccAgentTest.java

private static int findAvailablePort() throws BindException {

    for (int p = DccAgentEndpoint.DEFAULT_PORT; p < 65535; p++) {

        try {//from   w w w. jav  a 2s .  co m
            new ServerSocket(p);
            return p;
        } catch (IOException e) {
        }
    }

    throw new BindException("unable to find an available port");

}

From source file:com.github.hrpc.rpc.Server.java

public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Option conf,
        String rangeConf) throws IOException {
    try {/*from  w w  w  . ja  va2  s .c o  m*/
        IntegerRanges range = null;
        if (rangeConf != null) {
            range = conf.getRange(rangeConf, "");
        }
        if (range == null || range.isEmpty() || (address.getPort() != 0)) {
            socket.bind(address, backlog);
        } else {
            for (Integer port : range) {
                if (socket.isBound())
                    break;
                try {
                    InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port);
                    socket.bind(temp, backlog);
                } catch (BindException e) {
                    //Ignored
                }
            }
            if (!socket.isBound()) {
                throw new BindException("Could not find a free port in " + range);
            }
        }
    } catch (SocketException e) {
        throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e);
    }
}