Example usage for java.net DatagramSocket DatagramSocket

List of usage examples for java.net DatagramSocket DatagramSocket

Introduction

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

Prototype

public DatagramSocket(int port) throws SocketException 

Source Link

Document

Constructs a datagram socket and binds it to the specified port on the local host machine.

Usage

From source file:net.spfbl.dns.QueryDNS.java

/**
 * Configurao e intanciamento do servidor.
 * @throws java.net.SocketException se houver falha durante o bind.
 */// www . jav  a2  s .  com
public QueryDNS(int port) throws SocketException {
    super("SERVERDNS");
    setPriority(Thread.NORM_PRIORITY);
    // Criando conexes.
    Server.logDebug("binding DNS socket on port " + port + "...");
    PORT = port;
    SERVER_SOCKET = new DatagramSocket(port);
    Server.logTrace(getName() + " thread allocation.");
}

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

/**
 * Checks to see if a specific port is available.
 * // w  ww . j  a v a2  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: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 ww w  .j a va 2  s .  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:com.ejisto.util.IOUtils.java

private static boolean isPortAvailable(int port) {
    try (DatagramSocket udp = new DatagramSocket(port); ServerSocket tcp = new ServerSocket(port)) {
        tcp.setReuseAddress(true);//from  w  ww.ja va 2 s  .  co m
        udp.setReuseAddress(true);
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:org.wso2.carbon.analytics.common.jmx.agent.JmxAgentWebInterface.java

/**
 * Test the availability of the DataPublisher by
 * trying to connect to it (credentials are not checked)
 *
 * @return : whether the test was successful or not
 *///from   w w w . j a  v  a2 s .  c o m
public boolean testDataPublisherAvailability(String connectionType, String url, int port) {

    //check for tcp and ssl port availability
    if (connectionType.equalsIgnoreCase("tcp://") || connectionType.equalsIgnoreCase("ssl://")) {

        DatagramSocket ds = null;

        try {
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);

            return true;
        } catch (IOException e) {
            log.error(e);
        } finally {
            if (ds != null) {
                ds.close();
            }
        }
    }

    //check for http and https port availability
    if (connectionType.equalsIgnoreCase("http://") || connectionType.equalsIgnoreCase("https://")) {

        Socket socket = null;

        try {
            socket = new Socket();
            socket.setReuseAddress(true);

            SocketAddress sa = new InetSocketAddress(url, port);
            socket.connect(sa);
            return true;
        } catch (IOException e) {
            log.error(e);
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    //do nothing
                }
            }
        }
    }

    return false;
}

From source file:org.marketcetera.saclient.rpc.RpcSAClientImplTest.java

/**
 * Assigns a port value that is not in use.
 * /*from w ww .ja  v a 2 s. c  o  m*/
 * @return an <code>int</code> value
 */
private int assignPort() {
    for (int i = MIN_PORT_NUMBER; i <= MAX_PORT_NUMBER; i++) {
        try (ServerSocket ss = new ServerSocket(i)) {
            ss.setReuseAddress(true);
            try (DatagramSocket ds = new DatagramSocket(i)) {
                ds.setReuseAddress(true);
                return i;
            }
        } catch (IOException e) {
        }
    }
    throw new IllegalArgumentException("No available ports");
}

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

/**
 * Checks if a port is free.//from  ww  w.  j  av  a 2  s.c om
 * @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.openhab.binding.modbus.internal.TestCaseSupport.java

private void startUDPServer() throws UnknownHostException, InterruptedException {
    udpListener = new ModbusUDPListener(localAddress(), udpTerminalFactory);
    for (int portCandidate = 10000 + udpServerIndex.increment(); portCandidate < 20000; portCandidate++) {
        try {//from w w w.ja  v  a  2 s. com
            DatagramSocket socket = new DatagramSocket(portCandidate);
            socket.close();
            udpListener.setPort(portCandidate);
            break;
        } catch (SocketException e) {
            continue;
        }
    }

    udpListener.start();
    waitForUDPServerStartup();
    Assert.assertNotSame(-1, udpModbusPort);
    Assert.assertNotSame(0, udpModbusPort);
}

From source file:Reflector.java

private DatagramSocket initUnicastSocket() {
    // initialize a DatagramSocket

    DatagramSocket ds;/*from   w  w  w .j a v a 2  s  .  c  o m*/
    try {
        ds = new DatagramSocket(sendPort);
    } catch (Exception e) {
        e.printStackTrace();
        return (null);
    }
    return (ds);
}

From source file:Reflector.java

private DatagramSocket initUnicastSocket() {
    // initialize a DatagramSocket

    DatagramSocket ds;//from w  w  w.  ja  v  a 2  s .c  o  m
    try {
        ds = new DatagramSocket(listenPort);
    } catch (Exception e) {
        System.err.println("Failed to create DatagramSocket on " + "port " + listenPort);
        return (null);
    }
    return (ds);
}