Example usage for java.net DatagramSocket isClosed

List of usage examples for java.net DatagramSocket isClosed

Introduction

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

Prototype

public boolean isClosed() 

Source Link

Document

Returns whether the socket is closed or not.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//from w  w  w  . j  a v a 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);

        ds.send(dp);
        System.out.println(ds.isClosed());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * Validates that the socket is good.//from w ww. j a  va 2 s . c  om
 */
@Override
public boolean validateObject(SocketAddress key, DatagramSocket socket) {
    return socket.isBound() && !socket.isClosed() && socket.isConnected();
}

From source file:com.clustercontrol.poller.impl.UdpTransportMappingImpl.java

/**
 * Closes the socket and stops the listener thread.
 *
 * @throws IOException/*from   w  ww .ja  v a  2 s .c o m*/
 */
public void close() throws IOException {
    boolean interrupted = false;
    WorkerTask l = listener;
    if (l != null) {
        l.terminate();
        l.interrupt();
        if (socketTimeout > 0) {
            try {
                l.join();
            } catch (InterruptedException ex) {
                interrupted = true;
                logger.warn(ex);
            }
        }
        listener = null;
    }
    DatagramSocket closingSocket = socket;
    if ((closingSocket != null) && (!closingSocket.isClosed())) {
        closingSocket.close();
    }
    socket = null;
    if (interrupted) {
        Thread.currentThread().interrupt();
    }
}

From source file:com.clustercontrol.poller.impl.UdpTransportMappingImpl.java

/**
 * If receiving new datagrams fails with a {@link SocketException}, this
 * method is called to renew the socket - if possible.
 * //from  ww  w  .  j av a 2  s  . co m
 * @param socketException
 *            the exception that occurred.
 * @param failedSocket
 *            the socket that caused the exception. By default, he socket
 *            will be closed in order to be able to reopen it.
 *            Implementations may also try to reuse the socket, in
 *            dependence of the <code>socketException</code>.
 * @return the new socket or <code>null</code> if the listen thread should
 *         be terminated with the provided exception.
 * @throws SocketException
 *             a new socket exception if the socket could not be renewed.
 * @since 2.2.2
 */
protected DatagramSocket renewSocketAfterException(SocketException socketException, DatagramSocket failedSocket)
        throws SocketException {
    if ((failedSocket != null) && (!failedSocket.isClosed())) {
        failedSocket.close();
    }
    DatagramSocket s = new DatagramSocket(udpAddress.getPort(), udpAddress.getInetAddress());
    s.setSoTimeout(socketTimeout);
    return s;
}

From source file:org.echocat.jomon.net.dns.DnsServer.java

private static void receive(@Nonnull DatagramSocket sock, @Nonnull DatagramPacket indp) throws IOException {
    try {/*from w  w w .j  a  va 2 s  .  c  o m*/
        sock.receive(indp);
    } catch (final SocketException e) {
        if (sock.isClosed()) {
            final InterruptedIOException toThrow = new InterruptedIOException();
            toThrow.initCause(e);
            throw toThrow;
        } else {
            throw e;
        }
    }
}

From source file:org.mule.transport.udp.UdpSocketFactory.java

public boolean validateObject(Object key, Object object) {
    DatagramSocket socket = (DatagramSocket) object;
    return !socket.isClosed();
}