Example usage for java.net DatagramSocket close

List of usage examples for java.net DatagramSocket close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes this datagram socket.

Usage

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

/**
 * Checks to see if a specific port is available.
 *
 * @param port/*ww  w  . j  a  v a  2 s  .com*/
 *            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:com.barchart.udt.AppServer.java

private static InetAddress getLocalHostViaUdp() throws UnknownHostException {
    final InetSocketAddress sa = new InetSocketAddress("www.google.com", 80);

    DatagramSocket sock = null;
    try {//from  w  ww .  j a v  a2  s.c  o m
        sock = new DatagramSocket();
        sock.connect(sa);
        final InetAddress address = sock.getLocalAddress();
        return address;
    } catch (final SocketException e) {
        log.warn("Exception getting address", e);
        return InetAddress.getLocalHost();
    } finally {
        if (sock != null) {
            sock.close();
        }
    }
}

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 .  java 2s  . c  o m*/
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.apache.tajo.rpc.NettyServerBase.java

private static boolean available(int port) throws IOException {
    if (port < 1024 || port > 65535) {
        throw new IllegalArgumentException("Port Number Out of Bound: " + port);
    }/*from  w  w w. j ava  2s .  c o 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) {
        return false;
    } finally {
        if (ss != null) {
            ss.close();
        }

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

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

/**
 * From apache camel Checks to see if a specific port is available.
 * /*from  ww  w .j a v a2s .c  o 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:energy.usef.core.util.DateTimeUtil.java

private static synchronized String getUDPInfo(String message) {
    try {//from w  ww .  j  a  va  2s.co m
        LOGGER.debug("SENDING: {}", message);
        byte[] buf = message.getBytes();
        InetAddress address = InetAddress.getByName(serverIp);
        DatagramSocket socket = new DatagramSocket();
        socket.send(new DatagramPacket(buf, buf.length, address, port));
        DatagramPacket result = new DatagramPacket(new byte[PACKAGE_BUFFER], PACKAGE_BUFFER);
        socket.disconnect();
        socket.setSoTimeout(RESPONSE_TIMEOUT);
        socket.receive(result);
        socket.disconnect();
        socket.close();
        String resultStr = new String(result.getData()).trim();
        LOGGER.debug("RECEIVED: {} ", resultStr);
        errorCount = 0;
        return resultStr;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        errorCount++;
        if (errorCount >= MAX_ERROR_COUNT) {
            LOGGER.error("Unable to run simulation correctly.");
            System.exit(1);
        }
    }
    return null;
}

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

/**
 * Checks to see if a specific port is available.
 * /*from  w ww  .  j  a v a 2  s  .c  om*/
 * @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:net.mybox.mybox.Common.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *//*from w ww  .  ja va 2  s.c  om*/
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.eclipse.scanning.test.scan.nexus.MandelbrotRemoteTest.java

/**
 * Checks if a port is free.//  w  w  w .  j  a  v  a  2 s  . co 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:org.apache.hama.util.BSPNetUtils.java

/**
 * Checks to see if a specific port is available.
 * //w  ww.  j  av  a  2 s .  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;
}