public final class DatagramChannelUtil
extends java.lang.Object
DatagramChannel
s.Modifier and Type | Method and Description |
---|---|
static java.nio.channels.DatagramChannel |
openChannelInet4()
Opens a new
DatagramChannel . |
static void |
setIpMulticastLoopOption(java.nio.channels.DatagramChannel channel,
boolean loopBack)
Sets the IP multicast loop option on a datagram channel.
|
static void |
setIpMulticastTtlOption(java.nio.channels.DatagramChannel channel,
int ttl)
Sets the IP multicast ttl option on a datagram channel.
|
public static java.nio.channels.DatagramChannel openChannelInet4() throws java.io.IOException
Opens a new DatagramChannel
. If possible, this method will call
DatagramChannel.open(ProtocolFamily)
, passing
StandardProtocolFamily.INET
. This will limit the socket to
IPv4.
Unfortunately, the DatagramChannel.open(ProtocolFamily)
method is only available in JDK 1.7 and newer. In order to stay
compatible with JDK 1.6 (both at compile-time and run-time), this method
actually uses reflection for calling the
DatagramChannel.open(ProtocolFamily)
method. If the method
cannot be called (for example because the software is running on JRE 1.6
or reflection fails due to security restrictions), it falls back to
calling DatagramChannel.open()
. In this case, however, the
returned channel will not be restricted to IPv4.
Any exception thrown by DatagramChannel.open()
or
DatagramChannel.open(ProtocolFamily)
is also thrown by this
method. Any exception caused by reflection is caught by this method and
DatagramChannel.open()
is called directly instead.
DatagramChannel.open()
or DatagramChannel.open(StandardProtocolFamily.INET)
(the latter is preferred).java.io.IOException
- if the called open
method throws such an
exception.public static void setIpMulticastLoopOption(java.nio.channels.DatagramChannel channel, boolean loopBack) throws java.io.IOException
Sets the IP multicast loop option on a datagram channel.
When running on Java 7, this method calls
DatagramChannel.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, loopBack)
.
When running on older versions of Java (where the setOption
method and the StandardSocketOptions
class are missing, this
method does nothing.
channel
- channel on which the setOption
method should be
called.loopBack
- new value for the IP_MULTICAST_LOOP
flag.java.nio.channels.ClosedChannelException
- if setOption
throws such an exception.java.lang.IllegalArgumentException
- if setOption
throws such an exception.java.io.IOException
- if setOption
throws such an exception.java.lang.UnsupportedOperationException
- if setOption
throws such an exception.public static void setIpMulticastTtlOption(java.nio.channels.DatagramChannel channel, int ttl) throws java.io.IOException
Sets the IP multicast ttl option on a datagram channel.
When running on Java 7, this method calls
DatagramChannel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl)
.
When running on older versions of Java (where the setOption
method and the StandardSocketOptions
class are missing, this
method does nothing.
channel
- channel on which the setOption
method should be
called.ttl
- new value for the IP_MULTICAST_TTL
option.java.nio.channels.ClosedChannelException
- if setOption
throws such an exception.java.lang.IllegalArgumentException
- if setOption
throws such an exception.java.io.IOException
- if setOption
throws such an exception.java.lang.UnsupportedOperationException
- if setOption
throws such an exception.Copyright © 2014–2017 aquenos GmbH. All rights reserved.