APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.nio.ch

public Class DatagramSocketAdaptor

extends MulticastSocket
Class Inheritance
Imports
java.io.IOException, .InterruptedIOException, .UncheckedIOException, java.lang.invoke.MethodHandle, .MethodHandles, .MethodHandles.Lookup, .MethodType, java.net.DatagramPacket, .DatagramSocket, .InetAddress, .InetSocketAddress, .MulticastSocket, .NetworkInterface, .SocketAddress, .SocketException, .SocketOption, .SocketTimeoutException, .StandardSocketOptions, java.nio.channels.AlreadyConnectedException, .ClosedChannelException, .ClosedByInterruptException, .DatagramChannel, .MembershipKey, java.util.Objects, .Set

A multicast datagram socket based on a datagram channel. This class overrides every public method defined by java.net.DatagramSocket and java.net.MulticastSocket. The methods in this class are defined in exactly the same order as in java.net.DatagramSocket and java.net.MulticastSocket so as to simplify tracking changes.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
DatagramSocketAdaptor.DatagramSockets

Provides access to non-public constants in DatagramSocket.

private static class
DatagramSocketAdaptor.NetworkInterfaces

Defines static methods to invoke non-public NetworkInterface methods.

Field Summary

Modifier and TypeField and Description
private final DatagramChannelImpl
private InetAddress
private final Object
private NetworkInterface
private volatile int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private InetAddress
anyInetAddress()

Returns the InetAddress representing anyLocalAddress.

private NetworkInterface
anyNetworkInterface()

Returns the placeholder for "any network interface", its index is 0.

public void
bind(SocketAddress local)

Overrides java.net.DatagramSocket.bind.

Binds this DatagramSocket to a specific address and port.

private static InetAddress

Returns:

the multicast group
checkGroup
(SocketAddress mcastaddr)

Checks a SocketAddress to ensure that it is a multicast address.

public void
close()

Overrides java.net.DatagramSocket.close.

Implements java.io.Closeable.close.

Closes this datagram socket.

public void
connect(InetAddress address, int port)

Overrides java.net.DatagramSocket.connect.

Connects the socket to a remote address for this socket.

public void
connect(SocketAddress remote)

Overrides java.net.DatagramSocket.connect.

Connects this socket to a remote socket address (IP address + port number).

private void
pack-priv static DatagramSocket
private NetworkInterface
defaultNetworkInterface()

Returns the default NetworkInterface to use when joining or leaving a multicast group and a network interface is not specified.

public void
disconnect()

Overrides java.net.DatagramSocket.disconnect.

Disconnects the socket.

private boolean
public boolean
getBroadcast()

Overrides java.net.DatagramSocket.getBroadcast.

Tests if SO_BROADCAST is enabled.

public DatagramChannel
getChannel()

Overrides java.net.DatagramSocket.getChannel.

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

public InetAddress
getInetAddress()

Overrides java.net.DatagramSocket.getInetAddress.

Returns the address to which this socket is connected.

public InetAddress
getInterface()

Overrides java.net.MulticastSocket.getInterface.

Deprecated

Retrieve the address of the network interface used for multicast packets.

private int
public InetAddress
getLocalAddress()

Overrides java.net.DatagramSocket.getLocalAddress.

Gets the local address to which the socket is bound.

public int
getLocalPort()

Overrides java.net.DatagramSocket.getLocalPort.

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

public SocketAddress
getLocalSocketAddress()

Overrides java.net.DatagramSocket.getLocalSocketAddress.

Returns the address of the endpoint this socket is bound to.

public boolean
getLoopbackMode()

Overrides java.net.MulticastSocket.getLoopbackMode.

Deprecated

Get the setting for local loopback of multicast datagrams.

public NetworkInterface
getNetworkInterface()

Overrides java.net.MulticastSocket.getNetworkInterface.

Get the multicast network interface set for outgoing multicast datagrams sent from this socket.

public <T> T
getOption(SocketOption<T> name)

Overrides java.net.DatagramSocket.getOption.

Returns the value of a socket option.

public int
getPort()

Overrides java.net.DatagramSocket.getPort.

Returns the port number to which this socket is connected.

public int
getReceiveBufferSize()

Overrides java.net.DatagramSocket.getReceiveBufferSize.

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

public SocketAddress
getRemoteSocketAddress()

Overrides java.net.DatagramSocket.getRemoteSocketAddress.

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

public boolean
getReuseAddress()

Overrides java.net.DatagramSocket.getReuseAddress.

Tests if SO_REUSEADDR is enabled.

public int
getSendBufferSize()

Overrides java.net.DatagramSocket.getSendBufferSize.

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

public int
getSoTimeout()

Overrides java.net.DatagramSocket.getSoTimeout.

Retrieve setting for SO_TIMEOUT.

public int
getTimeToLive()

Overrides java.net.MulticastSocket.getTimeToLive.

Get the default time-to-live for multicast packets sent out on the socket.

public int
getTrafficClass()

Overrides java.net.DatagramSocket.getTrafficClass.

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

public boolean
isBound()

Overrides java.net.DatagramSocket.isBound.

Returns the binding state of the socket.

public boolean
isClosed()

Overrides java.net.DatagramSocket.isClosed.

Returns whether the socket is closed or not.

public boolean
isConnected()

Overrides java.net.DatagramSocket.isConnected.

Returns the connection state of the socket.

public void
joinGroup(InetAddress group)

Overrides java.net.MulticastSocket.joinGroup.

Deprecated

Joins a multicast group.

public void
joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)

Overrides java.net.MulticastSocket.joinGroup.

Joins a multicast group.

public void
leaveGroup(InetAddress group)

Overrides java.net.MulticastSocket.leaveGroup.

Deprecated

Leave a multicast group.

public void
leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)

Overrides java.net.MulticastSocket.leaveGroup.

Leave a multicast group on a specified local interface.

private NetworkInterface
outgoingNetworkInterface()

Returns the outgoing NetworkInterface or null if not set.

public void
receive(DatagramPacket p)

Overrides java.net.DatagramSocket.receive.

Receives a datagram packet from this socket.

public void
send(DatagramPacket p)

Overrides java.net.DatagramSocket.send.

Sends a datagram packet from this socket.

private void
setBooleanOption(SocketOption<Boolean> name, boolean value)

public void
setBroadcast(boolean on)

Overrides java.net.DatagramSocket.setBroadcast.

Enable/disable SO_BROADCAST.

public void
setInterface(InetAddress inf)

Overrides java.net.MulticastSocket.setInterface.

Deprecated

Set the multicast network interface used by methods whose behavior would be affected by the value of the network interface.

private void
setIntOption(SocketOption<Integer> name, int value)

public void
setLoopbackMode(boolean disable)

Overrides java.net.MulticastSocket.setLoopbackMode.

Deprecated

Disable/Enable local loopback of multicast datagrams.

public void
setNetworkInterface(NetworkInterface netIf)

Overrides java.net.MulticastSocket.setNetworkInterface.

Specify the network interface for outgoing multicast datagrams sent on this socket.

public <T> DatagramSocket
setOption(SocketOption<T> name, T value)

Overrides java.net.DatagramSocket.setOption.

Sets the value of a socket option.

public void
setReceiveBufferSize(int size)

Overrides java.net.DatagramSocket.setReceiveBufferSize.

Sets the SO_RCVBUF option to the specified value for this DatagramSocket.

public void
setReuseAddress(boolean on)

Overrides java.net.DatagramSocket.setReuseAddress.

Enable/disable the SO_REUSEADDR socket option.

public void
setSendBufferSize(int size)

Overrides java.net.DatagramSocket.setSendBufferSize.

Sets the SO_SNDBUF option to the specified value for this DatagramSocket.

public void
setSoTimeout(int timeout)

Overrides java.net.DatagramSocket.setSoTimeout.

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

public void
setTimeToLive(int ttl)

Overrides java.net.MulticastSocket.setTimeToLive.

Set the default time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.

public void
setTrafficClass(int tc)

Overrides java.net.DatagramSocket.setTrafficClass.

Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.

public Set<SocketOption<?>>
supportedOptions()

Overrides java.net.DatagramSocket.supportedOptions.

Returns a set of the socket options supported by this socket.

Field Detail

dcback to summary
private final DatagramChannelImpl dc
outgoingInetAddressback to summary
private InetAddress outgoingInetAddress
outgoingInterfaceLockback to summary
private final Object outgoingInterfaceLock
outgoingNetworkInterfaceback to summary
private NetworkInterface outgoingNetworkInterface
timeoutback to summary
private volatile int timeout

Constructor Detail

DatagramSocketAdaptorback to summary
private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException

Method Detail

anyInetAddressback to summary
private InetAddress anyInetAddress()

Returns the InetAddress representing anyLocalAddress.

anyNetworkInterfaceback to summary
private NetworkInterface anyNetworkInterface()

Returns the placeholder for "any network interface", its index is 0.

bindback to summary
public void bind(SocketAddress local) throws SocketException

Overrides java.net.DatagramSocket.bind.

Doc from java.net.DatagramSocket.bind.

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.

Annotations
@Override
Exceptions
SocketException:
if any error happens during the bind, or if the socket is already bound or is closed.
checkGroupback to summary
private static InetAddress checkGroup(SocketAddress mcastaddr) throws SocketException

Checks a SocketAddress to ensure that it is a multicast address.

Returns:InetAddress

the multicast group

Exceptions
SocketException:
if group is not a multicast address
IllegalArgumentException:
if group is null, an unsupported address type, or an unresolved address
closeback to summary
public void close()

Overrides java.net.DatagramSocket.close.

Implements java.io.Closeable.close.

Doc from java.net.DatagramSocket.close.

Closes this datagram socket.

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

If this socket has an associated channel then the channel is closed as well.

Once closed, several of the methods defined by this class will throw an exception if invoked on the closed socket.

Annotations
@Override
connectback to summary
public void connect(InetAddress address, int port)

Overrides java.net.DatagramSocket.connect.

Doc from java.net.DatagramSocket.connect.

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 socket is already closed, then this method has no effect.

If this socket is not bound then this method will first cause the socket to be bound to an address that is assigned automatically, as if invoking the bind method with a parameter of null. 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.

If this socket is already connected, then this method will attempt to connect to the given address. If this connect fails then the state of this socket is unknown - it may or may not be connected to the address that it was previously connected to.

When the socket is connected, the send method checks that the packet's address matches the remote address that the socket is connected to. A socket connected to a multicast address may only be used to send packets. Datagrams in the socket's socket receive buffer, which have not been received before invoking this method, may be discarded.

Annotations
@Override
connectback to summary
public void connect(SocketAddress remote) throws SocketException

Overrides java.net.DatagramSocket.connect.

Doc from java.net.DatagramSocket.connect.

Connects this socket to a remote socket address (IP address + port number).

If given an InetSocketAddress, this method behaves as if invoking connect(InetAddress,int) with the given socket addresses IP address and port number, except that the SocketException that may be raised is not wrapped in an UncheckedIOException. Datagrams in the socket's socket receive buffer, which have not been received before invoking this method, may be discarded.

If this socket is already connected, then this method will attempt to connect to the given address. If this connect fails then the state of this socket is unknown - it may or may not be connected to the address that it was previously connected to.

Annotations
@Override
Exceptions
SocketException:
if the connect fails
connectInternalback to summary
private void connectInternal(SocketAddress remote) throws SocketException
createback to summary
pack-priv static DatagramSocket create(DatagramChannelImpl dc)
defaultNetworkInterfaceback to summary
private NetworkInterface defaultNetworkInterface() throws SocketException

Returns the default NetworkInterface to use when joining or leaving a multicast group and a network interface is not specified. This method will return the outgoing NetworkInterface if set, otherwise the result of NetworkInterface.getDefault(), otherwise a NetworkInterface with index == 0 as a placeholder for "any network interface".

disconnectback to summary
public void disconnect()

Overrides java.net.DatagramSocket.disconnect.

Doc from java.net.DatagramSocket.disconnect.

Disconnects the socket. If the socket is closed or not connected, then this method has no effect.

Annotations
@Override
getBooleanOptionback to summary
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException
getBroadcastback to summary
public boolean getBroadcast() throws SocketException

Overrides java.net.DatagramSocket.getBroadcast.

Doc from java.net.DatagramSocket.getBroadcast.

Tests if SO_BROADCAST is enabled.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
getChannelback to summary
public DatagramChannel getChannel()

Overrides java.net.DatagramSocket.getChannel.

Doc from java.net.DatagramSocket.getChannel.

Returns the unique java.nio.channels.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 DatagramChannel.open method.

Annotations
@Override
getInetAddressback to summary
public InetAddress getInetAddress()

Overrides java.net.DatagramSocket.getInetAddress.

Doc from java.net.DatagramSocket.getInetAddress.

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

If the socket was connected prior to being closed, then this method will continue to return the connected address after the socket is closed.

Annotations
@Override
getInterfaceback to summary
public InetAddress getInterface() throws SocketException

Overrides java.net.MulticastSocket.getInterface.

Doc from java.net.MulticastSocket.getInterface.

Deprecated

Retrieve the address of the network interface used for multicast packets.

Annotations
@Override
@Deprecated
Exceptions
SocketException:
if there is an error in the underlying protocol, such as a TCP error, or the socket is closed.
getIntOptionback to summary
private int getIntOption(SocketOption<Integer> name) throws SocketException
getLocalAddressback to summary
public InetAddress getLocalAddress()

Overrides java.net.DatagramSocket.getLocalAddress.

Doc from java.net.DatagramSocket.getLocalAddress.

Gets the local address to which the socket is bound.

If the socket was initially bound to the wildcard address and is now connected, then the address returned may be the local address selected as the source address for datagrams sent on the socket instead of the wildcard address. When disconnect() is called, the bound address reverts to the wildcard address.

Annotations
@Override
getLocalPortback to summary
public int getLocalPort()

Overrides java.net.DatagramSocket.getLocalPort.

Doc from java.net.DatagramSocket.getLocalPort.

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

Annotations
@Override
getLocalSocketAddressback to summary
public SocketAddress getLocalSocketAddress()

Overrides java.net.DatagramSocket.getLocalSocketAddress.

Doc from java.net.DatagramSocket.getLocalSocketAddress.

Returns the address of the endpoint this socket is bound to.

If the socket was initially bound to the wildcard address and is now connected, then the address returned may be the local address selected as the source address for datagrams sent on this socket instead of the wildcard address. When disconnect() is called, the bound address reverts to the wildcard address.

Annotations
@Override
getLoopbackModeback to summary
public boolean getLoopbackMode() throws SocketException

Overrides java.net.MulticastSocket.getLoopbackMode.

Doc from java.net.MulticastSocket.getLoopbackMode.

Deprecated

Get the setting for local loopback of multicast datagrams.

Annotations
@Override
@Deprecated
Exceptions
SocketException:
if an error occurs while getting the value, or the socket is closed.
getNetworkInterfaceback to summary
public NetworkInterface getNetworkInterface() throws SocketException

Overrides java.net.MulticastSocket.getNetworkInterface.

Doc from java.net.MulticastSocket.getNetworkInterface.

Get the multicast network interface set for outgoing multicast datagrams sent from this socket.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as a TCP error, or the socket is closed.
getOptionback to summary
public <T> T getOption(SocketOption<T> name) throws IOException

Overrides java.net.DatagramSocket.getOption.

Doc from java.net.DatagramSocket.getOption.

Returns the value of a socket option.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or if the socket is closed.
getPortback to summary
public int getPort()

Overrides java.net.DatagramSocket.getPort.

Doc from java.net.DatagramSocket.getPort.

Returns the port number to which this socket is connected. Returns -1 if the socket is not connected.

If the socket was connected prior to being closed, then this method will continue to return the connected port number after the socket is closed.

Annotations
@Override
getReceiveBufferSizeback to summary
public int getReceiveBufferSize() throws SocketException

Overrides java.net.DatagramSocket.getReceiveBufferSize.

Doc from java.net.DatagramSocket.getReceiveBufferSize.

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

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
getRemoteSocketAddressback to summary
public SocketAddress getRemoteSocketAddress()

Overrides java.net.DatagramSocket.getRemoteSocketAddress.

Doc from java.net.DatagramSocket.getRemoteSocketAddress.

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

If the socket was connected prior to being closed, then this method will continue to return the connected address after the socket is closed.

Annotations
@Override
getReuseAddressback to summary
public boolean getReuseAddress() throws SocketException

Overrides java.net.DatagramSocket.getReuseAddress.

Doc from java.net.DatagramSocket.getReuseAddress.

Tests if SO_REUSEADDR is enabled.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
getSendBufferSizeback to summary
public int getSendBufferSize() throws SocketException

Overrides java.net.DatagramSocket.getSendBufferSize.

Doc from java.net.DatagramSocket.getSendBufferSize.

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

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
getSoTimeoutback to summary
public int getSoTimeout() throws SocketException

Overrides java.net.DatagramSocket.getSoTimeout.

Doc from java.net.DatagramSocket.getSoTimeout.

Retrieve setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
getTimeToLiveback to summary
public int getTimeToLive() throws IOException

Overrides java.net.MulticastSocket.getTimeToLive.

Doc from java.net.MulticastSocket.getTimeToLive.

Get the default time-to-live for multicast packets sent out on the socket.

Annotations
@Override
Exceptions
IOException:
if an I/O exception occurs while getting the default time-to-live value, or the socket is closed.
getTrafficClassback to summary
public int getTrafficClass() throws SocketException

Overrides java.net.DatagramSocket.getTrafficClass.

Doc from java.net.DatagramSocket.getTrafficClass.

Gets 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(int) this method may return a different value than was previously set using the setTrafficClass(int) method on this DatagramSocket.

Annotations
@Override
Exceptions
SocketException:
if there is an error obtaining the traffic class or type-of-service value, or the socket is closed.
isBoundback to summary
public boolean isBound()

Overrides java.net.DatagramSocket.isBound.

Doc from java.net.DatagramSocket.isBound.

Returns the binding state of the socket.

If the socket was bound prior to being closed, then this method will continue to return true after the socket is closed.

Annotations
@Override
isClosedback to summary
public boolean isClosed()

Overrides java.net.DatagramSocket.isClosed.

Doc from java.net.DatagramSocket.isClosed.

Returns whether the socket is closed or not.

Annotations
@Override
isConnectedback to summary
public boolean isConnected()

Overrides java.net.DatagramSocket.isConnected.

Doc from java.net.DatagramSocket.isConnected.

Returns the connection state of the socket.

If the socket was connected prior to being closed, then this method will continue to return true after the socket is closed.

Annotations
@Override
joinGroupback to summary
public void joinGroup(InetAddress group) throws IOException

Overrides java.net.MulticastSocket.joinGroup.

Doc from java.net.MulticastSocket.joinGroup.

Deprecated

Joins a multicast group. Its behavior may be affected by setInterface or setNetworkInterface.

Annotations
@Override
@Deprecated
Exceptions
IOException:
if there is an error joining, or when the address is not a multicast address, or the platform does not support multicasting, or the socket is closed.
joinGroupback to summary
public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IOException

Overrides java.net.MulticastSocket.joinGroup.

Doc from java.net.MulticastSocket.joinGroup.

Joins a multicast group.

In order to join a multicast group, the caller should specify the IP address of the multicast group to join, and the local network interface to receive multicast packets from.

  • The mcastaddr argument indicates the IP address of the multicast group to join. For historical reasons this is specified as a SocketAddress. The default implementation only supports InetSocketAddress and the port information is ignored.
  • The netIf argument specifies the local interface to receive multicast datagram packets, or null to defer to the interface set for outgoing multicast datagrams. If null, and no interface has been set, the behaviour is unspecified: any interface may be selected or the operation may fail with a SocketException.

It is possible to call this method several times to join several different multicast groups, or join the same group in several different networks. However, if the socket is already a member of the group, an IOException will be thrown.

Annotations
@Override
Exceptions
IOException:
if there is an error joining, or when the address is not a multicast address, or the platform does not support multicasting, or the socket is closed
leaveGroupback to summary
public void leaveGroup(InetAddress group) throws IOException

Overrides java.net.MulticastSocket.leaveGroup.

Doc from java.net.MulticastSocket.leaveGroup.

Deprecated

Leave a multicast group. Its behavior may be affected by setInterface or setNetworkInterface.

Annotations
@Override
@Deprecated
Exceptions
IOException:
if there is an error leaving or when the address is not a multicast address, or the socket is closed.
leaveGroupback to summary
public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IOException

Overrides java.net.MulticastSocket.leaveGroup.

Doc from java.net.MulticastSocket.leaveGroup.

Leave a multicast group on a specified local interface.

Annotations
@Override
Exceptions
IOException:
if there is an error leaving or when the address is not a multicast address, or the socket is closed.
outgoingNetworkInterfaceback to summary
private NetworkInterface outgoingNetworkInterface() throws SocketException

Returns the outgoing NetworkInterface or null if not set.

receiveback to summary
public void receive(DatagramPacket p) throws IOException

Overrides java.net.DatagramSocket.receive.

Doc from java.net.DatagramSocket.receive.

Receives a datagram packet from this socket. This method blocks until a datagram is received. 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. 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.

This method is interruptible in the following circumstances:

  1. The datagram socket is associated with a DatagramChannel. In that case, interrupting a thread receiving a datagram packet will close the underlying channel and cause this method to throw java.nio.channels.ClosedByInterruptException with the thread's interrupted status set.
  2. The datagram socket uses the system-default socket implementation and a virtual thread is receiving a datagram packet. In that case, interrupting the virtual thread will cause it to wakeup and close the socket. This method will then throw SocketException with the thread's interrupted status set.
Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or the socket is closed.
sendback to summary
public void send(DatagramPacket p) throws IOException

Overrides java.net.DatagramSocket.send.

Doc from java.net.DatagramSocket.send.

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.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or the socket is closed.
setBooleanOptionback to summary
private void setBooleanOption(SocketOption<Boolean> name, boolean value) throws SocketException
setBroadcastback to summary
public void setBroadcast(boolean on) throws SocketException

Overrides java.net.DatagramSocket.setBroadcast.

Doc from java.net.DatagramSocket.setBroadcast.

Enable/disable SO_BROADCAST.

Some operating systems may require that the Java virtual machine be started with implementation specific privileges to enable this option or send broadcast datagrams.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
setInterfaceback to summary
public void setInterface(InetAddress inf) throws SocketException

Overrides java.net.MulticastSocket.setInterface.

Doc from java.net.MulticastSocket.setInterface.

Deprecated

Set the multicast network interface used by methods whose behavior would be affected by the value of the network interface. Useful for multihomed hosts.

Annotations
@Override
@Deprecated
Exceptions
SocketException:
if there is an error in the underlying protocol, such as a TCP error, or the socket is closed.
setIntOptionback to summary
private void setIntOption(SocketOption<Integer> name, int value) throws SocketException
setLoopbackModeback to summary
public void setLoopbackMode(boolean disable) throws SocketException

Overrides java.net.MulticastSocket.setLoopbackMode.

Doc from java.net.MulticastSocket.setLoopbackMode.

Deprecated

Disable/Enable local loopback of multicast datagrams. The option is used by the platform's networking code as a hint for setting whether multicast data will be looped back to the local socket.

Because this option is a hint, applications that want to verify what loopback mode is set to should call getLoopbackMode()

Annotations
@Override
@Deprecated
Exceptions
SocketException:
if an error occurs while setting the value, or the socket is closed.
setNetworkInterfaceback to summary
public void setNetworkInterface(NetworkInterface netIf) throws SocketException

Overrides java.net.MulticastSocket.setNetworkInterface.

Doc from java.net.MulticastSocket.setNetworkInterface.

Specify the network interface for outgoing multicast datagrams sent on this socket.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as a TCP error, or the socket is closed.
setOptionback to summary
public <T> DatagramSocket setOption(SocketOption<T> name, T value) throws IOException

Overrides java.net.DatagramSocket.setOption.

Doc from java.net.DatagramSocket.setOption.

Sets the value of a socket option.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or if the socket is closed.
setReceiveBufferSizeback to summary
public void setReceiveBufferSize(int size) throws SocketException

Overrides java.net.DatagramSocket.setReceiveBufferSize.

Doc from java.net.DatagramSocket.setReceiveBufferSize.

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().

Increasing SO_RCVBUF may allow the network implementation to buffer multiple packets when packets arrive faster than are being received using receive(DatagramPacket).

Note

It is implementation specific if a packet larger than SO_RCVBUF can be received.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
setReuseAddressback to summary
public void setReuseAddress(boolean on) throws SocketException

Overrides java.net.DatagramSocket.setReuseAddress.

Doc from java.net.DatagramSocket.setReuseAddress.

Enable/disable the SO_REUSEADDR socket option.

For UDP sockets it may be necessary to bind more than one socket to the same socket address. This is typically for the purpose of receiving multicast packets (See java.net.MulticastSocket). The SO_REUSEADDR socket option allows multiple sockets to be bound to the same socket address if the SO_REUSEADDR socket option is enabled prior to binding the socket using bind(SocketAddress).

Note

This functionality is not supported by all existing platforms, so it is implementation specific whether this option will be ignored or not. However, if it is not supported then getReuseAddress() will always return false.

When a DatagramSocket is created the initial setting of SO_REUSEADDR is disabled.

The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See isBound()) is not defined.

Annotations
@Override
Exceptions
SocketException:
if an error occurs enabling or disabling the SO_REUSEADDR socket option, or the socket is closed.
setSendBufferSizeback to summary
public void setSendBufferSize(int size) throws SocketException

Overrides java.net.DatagramSocket.setSendBufferSize.

Doc from java.net.DatagramSocket.setSendBufferSize.

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.

Note

If send(DatagramPacket) is used to send a DatagramPacket that is larger than the setting of SO_SNDBUF then it is implementation specific if the packet is sent or discarded.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
setSoTimeoutback to summary
public void setSoTimeout(int timeout) throws SocketException

Overrides java.net.DatagramSocket.setSoTimeout.

Doc from java.net.DatagramSocket.setSoTimeout.

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a positive timeout value, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid. A timeout of zero is interpreted as an infinite timeout. The option must be enabled prior to entering the blocking operation to have effect.

Annotations
@Override
Exceptions
SocketException:
if there is an error in the underlying protocol, such as an UDP error, or the socket is closed.
setTimeToLiveback to summary
public void setTimeToLive(int ttl) throws IOException

Overrides java.net.MulticastSocket.setTimeToLive.

Doc from java.net.MulticastSocket.setTimeToLive.

Set the default time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.

The ttl must be in the range 0 <= ttl <= 255 or an IllegalArgumentException will be thrown. Multicast packets sent with a TTL of 0 are not transmitted on the network but may be delivered locally.

Annotations
@Override
Exceptions
IOException:
if an I/O exception occurs while setting the default time-to-live value, or the socket is closed.
setTrafficClassback to summary
public void setTrafficClass(int tc) throws SocketException

Overrides java.net.DatagramSocket.setTrafficClass.

Doc from java.net.DatagramSocket.setTrafficClass.

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.

The tc must be in the range 0 <= tc <= 255 or an IllegalArgumentException will be thrown.

Notes

For Internet Protocol v4 the value consists of an integer, the least significant 8 bits of which represent the value of the TOS octet in IP packets sent by the socket. RFC 1349 defines the TOS values as follows:

  • IPTOS_LOWCOST (0x02)
  • IPTOS_RELIABILITY (0x04)
  • IPTOS_THROUGHPUT (0x08)
  • IPTOS_LOWDELAY (0x10)
The last low order bit is always ignored as this corresponds to the MBZ (must be zero) bit.

Setting bits in the precedence field may result in a SocketException indicating that the operation is not permitted.

for Internet Protocol v6 tc is the value that would be placed into the sin6_flowinfo field of the IP header.

Annotations
@Override
Exceptions
SocketException:
if there is an error setting the traffic class or type-of-service, or the socket is closed.
supportedOptionsback to summary
public Set<SocketOption<?>> supportedOptions()

Overrides java.net.DatagramSocket.supportedOptions.

Doc from java.net.DatagramSocket.supportedOptions.

Returns a set of the socket options supported by this socket. This method will continue to return the set of options even after the socket has been closed.

Annotations
@Override
sun.nio.ch back to summary

private Class DatagramSocketAdaptor.DatagramSockets

extends Object
Class Inheritance

Provides access to non-public constants in DatagramSocket.

Field Summary

Modifier and TypeField and Description
private static final SocketAddress

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NO_DELEGATEback to summary
private static final SocketAddress NO_DELEGATE

Constructor Detail

DatagramSocketsback to summary
private DatagramSockets()
sun.nio.ch back to summary

private Class DatagramSocketAdaptor.NetworkInterfaces

extends Object
Class Inheritance

Defines static methods to invoke non-public NetworkInterface methods.

Field Summary

Modifier and TypeField and Description
pack-priv static final MethodHandle
pack-priv static final MethodHandle

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static NetworkInterface
getDefault()

Returns the default network interface or null.

pack-priv static NetworkInterface
newNetworkInterface(String name, int index, InetAddress[] addrs)

Creates a NetworkInterface with the given name index and addresses.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONSTRUCTORback to summary
pack-priv static final MethodHandle CONSTRUCTOR
GET_DEFAULTback to summary
pack-priv static final MethodHandle GET_DEFAULT

Constructor Detail

NetworkInterfacesback to summary
private NetworkInterfaces()

Method Detail

getDefaultback to summary
pack-priv static NetworkInterface getDefault()

Returns the default network interface or null.

newNetworkInterfaceback to summary
pack-priv static NetworkInterface newNetworkInterface(String name, int index, InetAddress[] addrs)

Creates a NetworkInterface with the given name index and addresses.