Example usage for java.net DatagramSocket isBound

List of usage examples for java.net DatagramSocket isBound

Introduction

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

Prototype

public boolean isBound() 

Source Link

Document

Returns the binding state of the socket.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {/*from  w w w . j a  va 2 s. c o  m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.isBound());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.googlecode.jmxtrans.connections.DatagramSocketFactory.java

/**
 * Validates that the socket is good./*  www.  j  ava 2  s  . c o m*/
 */
@Override
public boolean validateObject(SocketAddress key, DatagramSocket socket) {
    return socket.isBound() && !socket.isClosed() && socket.isConnected();
}

From source file:com.bitbreeds.webrtc.dtls.DtlsMuxStunTransport.java

public DtlsMuxStunTransport(PeerConnection parent, DatagramSocket socket, int mtu) throws IOException {
    this.parent = parent;
    this.socket = socket;
    this.receiveLimit = mtu - IP_BYTES - UDP_BYTES;
    this.sendLimit = mtu - IP_MAX_BYTES - UDP_BYTES;
    if (!socket.isBound() || !socket.isConnected()) {
        throw new IllegalArgumentException("Unbound socket");
    }//from   ww  w . ja  v a  2s .c om
}

From source file:com.clustercontrol.agent.Agent.java

/**
 * ??awakeAgent?/*from  ww w.  j ava  2 s  . co m*/
 * Agent.properties???UDP?24005??????????(releaseLatch)
 * ????ReceiveTopic????Topic????
 */
public void waitAwakeAgent() {
    final int BUFSIZE = 1;

    byte[] buf = new byte[BUFSIZE];
    InetAddress cAddr; // ??IP
    int cPort; // ???
    DatagramSocket sock = null;
    boolean flag = true;
    int port = 24005;

    int awakeDelay = 1000;

    try {
        String awakeDelayStr = AgentProperties.getProperty("awake.delay", Integer.toString(1000));
        awakeDelay = Integer.parseInt(awakeDelayStr);
        m_log.info("awake.delay = " + awakeDelay + " msec");
    } catch (NumberFormatException e) {
        m_log.error("awake.delay", e);
    }

    while (true) {
        /*
         * UDP???flag?true??
         * ?????flag?false?????getTopic(releaseLatch)?
         * 
         * UDP???????getTopic????????
         * ??????
         */
        try {
            if (sock != null && port != awakePort) {
                sock.close();
                sock = null;
            }
            if (sock == null || !sock.isBound()) {
                port = awakePort;
                sock = new DatagramSocket(port);
                sock.setSoTimeout(awakeDelay);
            }
            DatagramPacket recvPacket = new DatagramPacket(buf, BUFSIZE);
            sock.receive(recvPacket);
            cAddr = recvPacket.getAddress();
            cPort = recvPacket.getPort();
            flag = true;
            m_log.info("waitAwakeAgent (" + cAddr.getHostAddress() + " onPort=" + cPort + ") buf.length="
                    + buf.length);
        } catch (SocketTimeoutException e) {
            if (flag) {
                m_log.info("waitAwakeAgent packet end");
                m_receiveTopic.releaseLatch();
                flag = false;
            }
        } catch (Exception e) {
            String msg = "waitAwakeAgent port=" + awakePort + ", " + e.getClass().getSimpleName() + ", "
                    + e.getMessage();
            if (e instanceof BindException) {
                m_log.warn(msg);
            } else {
                m_log.warn(msg, e);
            }
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e1) {
                m_log.warn(e1, e1);
            }
        }
    }
}