Package 

Class SafeCloseDatagramSocket

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable

    
    public class SafeCloseDatagramSocket
    extends DelegatingDatagramSocket
                        

    Represents a DatagramSocket which fixes the following problem: when close is called while another thread is blocked in its receive, calling bind throws an "Already bound" exception until (the native counterpart of) the receive method returns.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      SafeCloseDatagramSocket() Initializes a new SafeCloseDatagramSocket instance and binds it to any available port on the local host machine.
      SafeCloseDatagramSocket(DatagramSocket delegate) Initializes a new SafeCloseDatagramSocket instance which to implement the DatagramSocket functionality by delegating to a specific DatagramSocket.
      SafeCloseDatagramSocket(int port) Initializes a new SafeCloseDatagramSocket instance and binds it to the specified port on the local host machine.
      SafeCloseDatagramSocket(int port, InetAddress laddr) Initializes a new SafeCloseDatagramSocket instance bound to the specified local address.
      SafeCloseDatagramSocket(SocketAddress bindaddr) Creates a datagram socket, bound to the specified local socket address.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      void close() Closes this datagram socket.
      void receive(DatagramPacket p) Receives a datagram packet from this socket.
      • Methods inherited from class org.ice4j.socket.DelegatingDatagramSocket

        bind, connect, connect, disconnect, getBroadcast, getChannel, getInetAddress, getLocalAddress, getLocalPort, getLocalSocketAddress, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, send, setBroadcast, setDefaultDelegateFactory, setDefaultReceiveBufferSize, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoTimeout, setTrafficClass
      • Methods inherited from class java.net.DatagramSocket

        bind, close, connect, disconnect, getBroadcast, getChannel, getInetAddress, getLocalAddress, getLocalPort, getLocalSocketAddress, getOption, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, receive, send, setBroadcast, setDatagramSocketImplFactory, setOption, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoTimeout, setTrafficClass, supportedOptions
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SafeCloseDatagramSocket

        SafeCloseDatagramSocket()
        Initializes a new SafeCloseDatagramSocket instance and binds it to any available port on the local host machine.
      • SafeCloseDatagramSocket

        SafeCloseDatagramSocket(DatagramSocket delegate)
        Initializes a new SafeCloseDatagramSocket instance which to implement the DatagramSocket functionality by delegating to a specific DatagramSocket.
        Parameters:
        delegate - the DatagramSocket to which the new instance is to delegate
      • SafeCloseDatagramSocket

        SafeCloseDatagramSocket(int port)
        Initializes a new SafeCloseDatagramSocket instance and binds it to the specified port on the local host machine.
        Parameters:
        port - the port to bind the new socket to
      • SafeCloseDatagramSocket

        SafeCloseDatagramSocket(int port, InetAddress laddr)
        Initializes a new SafeCloseDatagramSocket instance bound to the specified local address.
        Parameters:
        port - the local port to bind the new socket to
        laddr - the local address to bind the new socket to
      • SafeCloseDatagramSocket

        SafeCloseDatagramSocket(SocketAddress bindaddr)
        Creates a datagram socket, bound to the specified local socket address.
        Parameters:
        bindaddr - local socket address to bind, or null for an unbound socket
    • Method Detail

      • close

         void close()

        Closes this datagram socket.

        Any thread currently blocked in receive upon this socket will throw a SocketException and this datagram socket will wait for it to return.

      • receive

         void receive(DatagramPacket p)

        Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.

        This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.

        Parameters:
        p - the DatagramPacket into which to place the incoming data