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:Main.java

public static void sendWoLMagicPacket(final String broadcastIp, final String macAddress) {
    new Thread(new Runnable() {
        @Override//from  w ww  . j a  v  a  2s .c  o m
        public void run() {
            try {
                byte[] macBytes = getMacBytes(macAddress);
                byte[] bytes = new byte[6 + 16 * macBytes.length];
                for (int i = 0; i < 6; i++) {
                    bytes[i] = (byte) 0xff;
                }
                for (int i = 6; i < bytes.length; i += macBytes.length) {
                    System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
                }

                InetAddress address = InetAddress.getByName(broadcastIp);
                DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 9);
                DatagramSocket socket = new DatagramSocket();
                socket.send(packet);
                socket.close();

                packet = new DatagramPacket(bytes, bytes.length, address, 7);
                socket = new DatagramSocket();
                socket.send(packet);
                socket.close();

                Log.e("WAKE_UP", "Wake-on-LAN packet sent.");
                final String output = getStringFromBytes(bytes);
                Log.i("WAKE_UP", output);
            } catch (Exception e) {
                Log.e("WAKE_UP", "Failed to send Wake-on-LAN packet: " + e);

            }
        }
    }).start();
}

From source file:org.springframework.integration.ip.util.SocketUtils.java

public static int findAvailableUdpSocket(int seed) {
    for (int i = seed; i < seed + 200; i++) {
        try {/*  w  w  w  . ja  v  a2  s. c om*/
            DatagramSocket sock = new DatagramSocket(i);
            sock.close();
            Thread.sleep(100);
            return i;
        } catch (Exception e) {
        }
    }
    throw new RuntimeException("Cannot find a free server socket");
}

From source file:Main.java

/**
 * Checks to see if a specific port is available.
 * //from   w ww .ja v a 2 s .  c o m
 * @Author From apache Mina project
 * 
 * @param port
 *            the port to check for availability
 */
public static boolean portAvailable(final int port) {
    if (port < 1 || port > 30000) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

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

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

    return false;
}

From source file:Main.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 * Code from http://mina.apache.org//*from  w  w  w . j av a 2s . c o m*/
 */
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) {
        /* falls through */
    } 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.image32.demo.simpleapi.SimpleApiDemo.java

public static boolean checkPortAvailablity(int port) {

    ServerSocket ss = null;/*from www  .j av a  2  s  . c o  m*/
    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) {
                return false;
            }
        }
    }
    return false;
}

From source file:AvailablePortFinder.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *//*from  www  . j  av  a 2  s .co m*/
public static boolean available(int port) {
    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:com.centeractive.ws.server.SimpleServerTest.java

public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;/*from w w  w . j a  v  a2 s.  c om*/
    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) {
            }
        }
    }
    return false;
}

From source file:org.exoplatform.addons.es.integration.BaseIntegrationTest.java

/**
 * Get a random available port/*from   w w w.  j  av  a 2s .  co  m*/
 * @return
 * @throws IOException
 */
private static int getAvailablePort() throws IOException {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(0);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(0);
        ds.setReuseAddress(true);
        return ss.getLocalPort();
    } finally {
        if (ds != null) {
            ds.close();
        }

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

From source file:it.geosolutions.geostore.services.rest.SecurityTest.java

/**
 * Checks if a network host / port is already occupied.
 * //from ww w  .  ja v a 2s.  c o m
 * @param host
 * @param port
 * @return
 */
private static boolean portIsBusy(String host, int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return false;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return true;
}

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);
    }//from  w  w w  .  ja  v a  2s  .com

    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;
}