Example usage for java.net DatagramSocket setReuseAddress

List of usage examples for java.net DatagramSocket setReuseAddress

Introduction

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

Prototype

public synchronized void setReuseAddress(boolean on) throws SocketException 

Source Link

Document

Enable/disable the SO_REUSEADDR socket option.

Usage

From source file:fr.fastconnect.factory.tibco.bw.maven.bwengine.AbstractServiceEngineMojo.java

public static boolean available(int port, int minPort, int maxPort) {
    if (port < minPort || port > maxPort || port > 65535) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }//www  .  j  av a2s .  co  m

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.jumpmind.util.AppUtils.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port/*www  .  j  a  v  a2 s. c  om*/
 *            the port to check for availability
 */
public static boolean isPortAvailable(int port) {
    if (port < 1 || port > 65535) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.wso2.carbon.device.mgt.iot.agent.firealarm.transport.TransportUtils.java

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

    try {//from  w  ww. ja v a2s .  c  om
        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;
}

From source file:acoli.controller.Controller.java

/**
 * http://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *///from w  ww.j ava  2s. com
public static boolean portAvailable(int port) {
    int MIN_PORT_NUMBER = 1000;
    int MAX_PORT_NUMBER = 9999;
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return false;
}

From source file:org.messic.service.MessicMain.java

/**
 * From apache camel Checks to see if a specific port is available.
 * /* ww w. j ava2  s . co m*/
 * @param port the port to check for availability
 */
public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    }

    finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.kawanfw.sql.tomcat.TomcatStarterUtil.java

/**
 * Checks to see if a specific port is available.
 * /*from w w  w .ja  v  a  2  s.co  m*/
 * @param port
 *            the port to check for availability
 */
public static boolean available(int port) {

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
        // e.printStackTrace();
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.eclipse.scanning.test.scan.nexus.MandelbrotRemoteTest.java

/**
 * Checks if a port is free./*from w  w w .j a v  a 2s  .c  o  m*/
 * @param port
 * @return
 */
public static boolean isPortFree(int port) {

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
        // Swallow this, it's not free
        return false;
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

}

From source file:net.mybox.mybox.Common.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *//*  ww  w. j a  va2 s .c  o m*/
public static boolean portAvailable(int port) {
    if (port < 0 || port > 65535) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.apache.hama.util.BSPNetUtils.java

/**
 * Checks to see if a specific port is available.
 * // w ww  . j  a v a 2s.  c  o m
 * @param port the port to check for availability
 */
public static boolean available(int port) {
    if (port < 1000 || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:poisondog.net.udp.SendMission.java

public Void execute(DatagramParameter parameter) throws IOException {
    DatagramSocket socket = new DatagramSocket();

    socket.setReuseAddress(true);
    socket.setBroadcast(parameter.getBroadcast());

    if (parameter.getTimeout() > 0)
        socket.setSoTimeout(parameter.getTimeout());

    String data = IOUtils.toString(parameter.getInputStream());
    DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length());
    packet.setAddress(InetAddress.getByName(parameter.getHost()));
    if (parameter.getPort() > 0)
        packet.setPort(parameter.getPort());
    socket.send(packet);/*from   www.  jav  a 2  s  . co  m*/
    socket.close();
    return null;
}