Package 

Class DelegatingDatagramSocket

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

    
    public class DelegatingDatagramSocket
    extends DatagramSocket
                        

    Implements a DatagramSocket which delegates its calls to a specific DatagramSocket.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static void setDefaultReceiveBufferSize(int size) Specifies a default receive buffer size for the underlying sockets.
      static void setDefaultDelegateFactory(DatagramSocketFactory factory) If a factory is provided, then any new instances of DelegatingDatagramSocket will automatically use a socket from the factory instead of the super class.
      void bind(SocketAddress addr) Binds this DatagramSocket to a specific address and port.
      void close() Closes this datagram socket.
      void connect(InetAddress address, int port) Connects the socket to a remote address for this socket.
      void connect(SocketAddress addr) Connects this socket to a remote socket address.
      void disconnect() Disconnects the socket.
      boolean getBroadcast() Tests if SO_BROADCAST is enabled.
      DatagramChannel getChannel() Returns the unique DatagramChannel object associated with this datagram socket, if any.
      InetAddress getInetAddress() Returns the address to which this socket is connected.
      InetAddress getLocalAddress() Gets the local address to which the socket is bound.
      int getLocalPort() Returns the port number on the local host to which this socket is bound.
      SocketAddress getLocalSocketAddress() Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
      int getPort() Returns the port for this socket.
      int getReceiveBufferSize() Gets the value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.
      SocketAddress getRemoteSocketAddress() Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
      boolean getReuseAddress() Tests if SO_REUSEADDR is enabled.
      int getSendBufferSize() Gets the value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket.
      int getSoTimeout() Retrieves setting for SO_TIMEOUT.
      int getTrafficClass() Gets the traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.
      boolean isBound() Returns the binding state of the socket.
      boolean isClosed() Returns whether the socket is closed or not.
      boolean isConnected() Returns the connection state of the socket.
      void receive(DatagramPacket p) Receives a datagram packet from this socket.
      void send(DatagramPacket p) Sends a datagram packet from this socket.
      void setBroadcast(boolean on) Enables/disables SO_BROADCAST.
      void setReceiveBufferSize(int size) Sets the SO_RCVBUF option to the specified value for this DatagramSocket.
      void setReuseAddress(boolean on) Enables/disables the SO_REUSEADDR socket option.
      void setSendBufferSize(int size) Sets the SO_SNDBUF option to the specified value for this DatagramSocket.
      void setSoTimeout(int timeout) Enables/disables SO_TIMEOUT with the specified timeout, in milliseconds.
      void setTrafficClass(int tc) Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.
      • Methods inherited from class java.net.DatagramSocket

        getOption, setDatagramSocketImplFactory, setOption, supportedOptions
      • Methods inherited from class java.lang.Object

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

      • DelegatingDatagramSocket

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

        DelegatingDatagramSocket(DatagramSocket delegate)
        Initializes a new DelegatingDatagramSocket 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
      • DelegatingDatagramSocket

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

        DelegatingDatagramSocket(int port, InetAddress laddr)
        Initializes a new DelegatingDatagramSocket 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
      • DelegatingDatagramSocket

        DelegatingDatagramSocket(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
      • DelegatingDatagramSocket

        DelegatingDatagramSocket(DatagramSocket delegate, SocketAddress address)
        Initializes a new DelegatingDatagramSocket instance which implements the DatagramSocket functionality.
        Parameters:
        delegate - the DatagramSocket to which the new instance is to delegate.
        address - The local socket address to bind, or null for an unbound socket.
    • Method Detail

      • setDefaultReceiveBufferSize

         static void setDefaultReceiveBufferSize(int size)

        Specifies a default receive buffer size for the underlying sockets. This function must be called before using the DelegatingDatagramSocket constructor. The size must be greater than 0 to be effective.

        Parameters:
        size - the size to which to set the receive buffer size.
      • setDefaultDelegateFactory

         static void setDefaultDelegateFactory(DatagramSocketFactory factory)

        If a factory is provided, then any new instances of DelegatingDatagramSocket will automatically use a socket from the factory instead of the super class. If this method is used, the factory class needs to ensure the socket objects are properly constructed and initialized - in particular, any default receive buffer size specified through DelegatingDatagramSocket#setDefaultReceiveBufferSize() won't automatically be applied to sockets obtain from the factory.

        Parameters:
        factory - The factory assigned to generates the new DatagramSocket for each new DelegatingDatagramSocket which do not use a delegate socket.
      • bind

         void bind(SocketAddress addr)

        Binds this DatagramSocket to a specific address and port.

        If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

        Parameters:
        addr - the address and port to bind to
      • close

         void close()

        Closes this datagram socket.

        Any thread currently blocked in receive upon this socket will throw a SocketException.

      • connect

         void connect(InetAddress address, int port)

        Connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected.

        If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown.

        Parameters:
        address - the remote address for the socket
        port - the remote port for the socket
      • connect

         void connect(SocketAddress addr)

        Connects this socket to a remote socket address.

        Parameters:
        addr - the remote address
      • disconnect

         void disconnect()

        Disconnects the socket. This does nothing if the socket is not connected.

      • getBroadcast

         boolean getBroadcast()

        Tests if SO_BROADCAST is enabled.

      • getChannel

         DatagramChannel getChannel()

        Returns the unique DatagramChannel object associated with this datagram socket, if any.

        A datagram socket will have a channel if, and only if, the channel itself was created via the open method

      • getInetAddress

         InetAddress getInetAddress()

        Returns the address to which this socket is connected. Returns null if the socket is not connected.

      • getLocalAddress

         InetAddress getLocalAddress()

        Gets the local address to which the socket is bound.

        If there is a security manager, its checkConnect method is first called with the host address and -1 as its arguments to see if the operation is allowed.

      • getLocalPort

         int getLocalPort()

        Returns the port number on the local host to which this socket is bound.

      • getPort

         int getPort()

        Returns the port for this socket. Returns -1 if the socket is not connected.

      • getReceiveBufferSize

         int getReceiveBufferSize()

        Gets the value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.

      • getReuseAddress

         boolean getReuseAddress()

        Tests if SO_REUSEADDR is enabled.

      • getSendBufferSize

         int getSendBufferSize()

        Gets the value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket.

      • getSoTimeout

         int getSoTimeout()

        Retrieves setting for SO_TIMEOUT. Zero returned implies that the option is disabled (i.e., timeout of infinity).

      • getTrafficClass

         int getTrafficClass()

        Gets the traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.

        As the underlying network implementation may ignore the traffic class or type-of-service set using setTrafficClass this method may return a different value than was previously set using the setTrafficClass method on this DatagramSocket.

      • isBound

         boolean isBound()

        Returns the binding state of the socket.

      • isClosed

         boolean isClosed()

        Returns whether the socket is closed or not.

      • isConnected

         boolean isConnected()

        Returns the connection state of the socket.

      • 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.

        If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it.

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

         void send(DatagramPacket p)

        Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.

        If there is a security manager, and the socket is not currently connected to a remote address, this method first performs some security checks. First, if p.getAddress().isMulticastAddress() is true, this method calls the security manager's checkMulticast method with p.getAddress() as its argument. If the evaluation of that expression is false, this method instead calls the security manager's checkConnect method with arguments p.getAddress().getHostAddress() and p.getPort(). Each call to a security manager method could result in a SecurityException if the operation is not allowed.

        Parameters:
        p - the DatagramPacket to be sent
      • setBroadcast

         void setBroadcast(boolean on)

        Enables/disables SO_BROADCAST.

        Parameters:
        on - whether or not to have broadcast turned on
      • setReceiveBufferSize

         void setReceiveBufferSize(int size)

        Sets the SO_RCVBUF option to the specified value for this DatagramSocket. The SO_RCVBUF option is used by the network implementation as a hint to size the underlying network I/O buffers. The SO_RCVBUF setting may also be used by the network implementation to determine the maximum size of the packet that can be received on this socket.

        Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize.

        Parameters:
        size - the size to which to set the receive buffer size.
      • setReuseAddress

         void setReuseAddress(boolean on)

        Enables/disables the SO_REUSEADDR socket option.

        Parameters:
        on - whether to enable or disable the SO_REUSEADDR socket option
      • setSendBufferSize

         void setSendBufferSize(int size)

        Sets the SO_SNDBUF option to the specified value for this DatagramSocket. The SO_SNDBUF option is used by the network implementation as a hint to size the underlying network I/O buffers. The SO_SNDBUF setting may also be used by the network implementation to determine the maximum size of the packet that can be sent on this socket.

        As SO_SNDBUF is a hint, applications that want to verify what size the buffer is should call getSendBufferSize.

        Increasing the buffer size may allow multiple outgoing packets to be queued by the network implementation when the send rate is high.

        Parameters:
        size - the size to which to set the send buffer size.
      • setSoTimeout

         void setSoTimeout(int timeout)

        Enables/disables SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive for this DatagramSocket will block for only this amount of time. If the timeout expires, a SocketTimeoutException is raised, though the DatagramSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be greater than zero. A timeout of zero is interpreted as an infinite timeout.

        Parameters:
        timeout - the specified timeout in milliseconds
      • setTrafficClass

         void setTrafficClass(int tc)

        Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket. As the underlying network implementation may ignore this value applications should consider it a hint.

        Parameters:
        tc - an int value for the bitset