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: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//*  ww  w .ja  v  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:org.vesalainen.net.sntp.SNTPServer.java

@Override
public void run() {
    try {/*from   w  w w.j a v a  2s  .co m*/
        DatagramSocket socket = new DatagramSocket(123);
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while (true) {
            socket.receive(packet);
            NtpV3Impl msg = new NtpV3Impl();
            msg.setDatagramPacket(packet);
            System.err.println(msg);
        }
    } catch (SocketException ex) {
        Logger.getLogger(SNTPServer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SNTPServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:werewolf.client.DatagramReceiverThread.java

public DatagramReceiverThread(int listenPort) {
    buf = new byte[4096];
    packet = new DatagramPacket(buf, buf.length);
    this.listenPort = listenPort;

    try {/*from  w  w  w .j  ava2 s  .  c o  m*/
        socket = new DatagramSocket(listenPort);
    } catch (SocketException ex) {
        Logger.getLogger(DatagramReceiverThread.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openhab.binding.edimax.internal.UDPDiscoverer.java

private EdimaxDevice[] discover() throws SocketException, UnknownHostException, IOException {
    List<EdimaxDevice> discoveredDevices = new ArrayList<EdimaxDevice>();
    DatagramSocket serverSocket = null;
    try {/*from  ww  w  .j a va 2s.  co  m*/
        serverSocket = new DatagramSocket(12346); // choose random port,
        // because with empty
        // port sometimes error
        // occures.

        // send UDP broadcast
        InetAddress ipAddress = InetAddress.getByName("255.255.255.255");
        byte[] sendData = DISCOVERY_BYTES;
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress, 20560);
        serverSocket.send(sendPacket);

        // receive
        serverSocket.setSoTimeout(1000 * 5);
        byte[] receiveData = new byte[1024];

        try {
            while (true) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

                serverSocket.receive(receivePacket);
                String sentence = new String(receivePacket.getData());

                if (!StringUtils.isEmpty(sentence) && sentence.contains("EDIMAX")) {
                    byte[] mac = new byte[6];
                    System.arraycopy(receivePacket.getData(), 0, mac, 0, 6);

                    String encodedMAC = Hex.encodeHexString(mac).toUpperCase();
                    InetAddress discoveredIp = receivePacket.getAddress();

                    EdimaxDevice dev = new EdimaxDevice();
                    dev.setIp(discoveredIp.getHostAddress());
                    dev.setMac(encodedMAC);

                    discoveredDevices.add(dev);
                }

            }
        } catch (SocketTimeoutException e) {
            // intended to happen
        }
    } finally {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }
    return discoveredDevices.toArray(new EdimaxDevice[discoveredDevices.size()]);
}

From source file:network.UDPServer.java

public UDPServer(int port, ServerHandler serverHandle) {
    this.port = port;
    this.serverHandler = serverHandle;
    try {/* ww w  .j  a  va 2  s  .c o m*/
        this.socket = new DatagramSocket(this.port);
        //this.executorService = Executors.newFixedThreadPool(50);
        this.executorService = Executors.newCachedThreadPool();
    } catch (SocketException e) {
        e.printStackTrace();
    }
}

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

/**
 * Checks if a network host / port is already occupied.
 * /*from   w w w  . ja va  2 s  .  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:werewolf_client.Werewolf_Client.java

public void recvFromClient(String adr, int p) throws SocketException, IOException {
    DatagramSocket recv = new DatagramSocket(p);
    byte[] recvData = new byte[1024];

    while (true) {
        DatagramPacket recvPacket = new DatagramPacket(recvData, recvData.length);
        recv.receive(recvPacket);//from www  . j  a  va 2 s  . c  o m

        String s = new String(recvPacket.getData(), 0, recvPacket.getLength());
        System.out.println("ReceivedD\n" + s);
    }
}

From source file:com.esri.geoevent.test.performance.ClockSync.java

@Override
public void run() {
    DatagramSocket socket = null;
    try {/* w ww.  j a v a  2s .  co m*/
        byte[] incomingBuffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(incomingBuffer, incomingBuffer.length);

        ByteBuffer bb = ByteBuffer.allocate(8);
        DatagramPacket outgoingPacket = new DatagramPacket(bb.array(), 0, 8, null, port);
        socket = new DatagramSocket(port);
        socket.setSoTimeout(100);
        while (isRunning.get()) {
            try {
                socket.receive(packet);
                long now = System.currentTimeMillis();
                bb.putLong(now);
                outgoingPacket.setAddress(packet.getAddress());
                outgoingPacket.setPort(packet.getPort());
                socket.send(outgoingPacket);
                bb.clear();
                //System.out.println("Sent the time " + now);
            } catch (SocketTimeoutException ex) {
                // Do nothing if nothing was sent.
            }
        }
    } catch (BindException e) {
        // port is in use - increment and try again
        port++;
        this.run();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
}

From source file:org.eredlab.g4.ccl.net.DefaultDatagramSocketFactory.java

/***
 * Creates a DatagramSocket on the local host at a specified port.
 * <p>/*from  w w w . j a va 2 s  . c o  m*/
 * @param port The port to use for the socket.
 * @exception SocketException If the socket could not be created.
 ***/
public DatagramSocket createDatagramSocket(int port) throws SocketException {
    return new DatagramSocket(port);
}

From source file:org.cloudfoundry.identity.statsd.integration.UaaMetricsEmitterIT.java

@Before
public void setUp() throws IOException {
    serverSocket = new DatagramSocket(8125);
    receiveData = new byte[65535];
    receivePacket = new DatagramPacket(receiveData, receiveData.length);
}