Path: blob/master/src/java.base/share/classes/java/nio/channels/MulticastChannel.java
41159 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.net.InetAddress;28import java.net.NetworkInterface;29import java.io.IOException;30import java.net.ProtocolFamily; // javadoc31import java.net.StandardProtocolFamily; // javadoc32import java.net.StandardSocketOptions; // javadoc3334/**35* A network channel that supports Internet Protocol (IP) multicasting.36*37* <p> IP multicasting is the transmission of IP datagrams to members of38* a <em>group</em> that is zero or more hosts identified by a single destination39* address.40*41* <p> In the case of a channel to an {@link StandardProtocolFamily#INET IPv4} socket,42* the underlying operating system optionally supports43* <a href="http://www.ietf.org/rfc/rfc2236.txt"> <i>RFC 2236: Internet Group44* Management Protocol, Version 2 (IGMPv2)</i></a>. When IGMPv2 is supported then45* the operating system may additionally support source filtering as specified by46* <a href="http://www.ietf.org/rfc/rfc3376.txt"> <i>RFC 3376: Internet Group47* Management Protocol, Version 3 (IGMPv3)</i></a>.48* For channels to an {@link StandardProtocolFamily#INET6 IPv6} socket, the equivalent49* standards are <a href="http://www.ietf.org/rfc/rfc2710.txt"> <i>RFC 2710:50* Multicast Listener Discovery (MLD) for IPv6</i></a> and <a51* href="http://www.ietf.org/rfc/rfc3810.txt"> <i>RFC 3810: Multicast Listener52* Discovery Version 2 (MLDv2) for IPv6</i></a>.53*54* <p> The {@link #join(InetAddress,NetworkInterface)} method is used to55* join a group and receive all multicast datagrams sent to the group. A channel56* may join several multicast groups and may join the same group on several57* {@link NetworkInterface interfaces}. Membership is dropped by invoking the {@link58* MembershipKey#drop drop} method on the returned {@link MembershipKey}. If the59* underlying platform supports source filtering then the {@link MembershipKey#block60* block} and {@link MembershipKey#unblock unblock} methods can be used to block or61* unblock multicast datagrams from particular source addresses.62*63* <p> The {@link #join(InetAddress,NetworkInterface,InetAddress)} method64* is used to begin receiving datagrams sent to a group whose source address matches65* a given source address. This method throws {@link UnsupportedOperationException}66* if the underlying platform does not support source filtering. Membership is67* <em>cumulative</em> and this method may be invoked again with the same group68* and interface to allow receiving datagrams from other source addresses. The69* method returns a {@link MembershipKey} that represents membership to receive70* datagrams from the given source address. Invoking the key's {@link71* MembershipKey#drop drop} method drops membership so that datagrams from the72* source address can no longer be received.73*74* <h2>Platform dependencies</h2>75*76* The multicast implementation is intended to map directly to the native77* multicasting facility. Consequently, the following items should be considered78* when developing an application that receives IP multicast datagrams:79*80* <ol>81*82* <li><p> The creation of the channel should specify the {@link ProtocolFamily}83* that corresponds to the address type of the multicast groups that the channel84* will join. There is no guarantee that a channel to a socket in one protocol85* family can join and receive multicast datagrams when the address of the86* multicast group corresponds to another protocol family. For example, it is87* implementation specific if a channel to an {@link StandardProtocolFamily#INET6 IPv6}88* socket can join an {@link StandardProtocolFamily#INET IPv4} multicast group and receive89* multicast datagrams sent to the group. </p></li>90*91* <li><p> The channel's socket should be bound to the {@link92* InetAddress#isAnyLocalAddress wildcard} address. If the socket is bound to93* a specific address, rather than the wildcard address then it is implementation94* specific if multicast datagrams are received by the socket. </p></li>95*96* <li><p> The {@link StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} option should be97* enabled prior to {@link NetworkChannel#bind binding} the socket. This is98* required to allow multiple members of the group to bind to the same99* address. </p></li>100*101* </ol>102*103* <p> <b>Usage Example:</b>104* <pre>105* // join multicast group on this interface, and also use this106* // interface for outgoing multicast datagrams107* NetworkInterface ni = NetworkInterface.getByName("hme0");108*109* DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)110* .setOption(StandardSocketOptions.SO_REUSEADDR, true)111* .bind(new InetSocketAddress(5000))112* .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);113*114* InetAddress group = InetAddress.getByName("225.4.5.6");115*116* MembershipKey key = dc.join(group, ni);117* </pre>118*119* @since 1.7120*/121122public interface MulticastChannel123extends NetworkChannel124{125/**126* Closes this channel.127*128* <p> If the channel is a member of a multicast group then the membership129* is {@link MembershipKey#drop dropped}. Upon return, the {@link130* MembershipKey membership-key} will be {@link MembershipKey#isValid131* invalid}.132*133* <p> This method otherwise behaves exactly as specified by the {@link134* Channel} interface.135*136* @throws IOException137* If an I/O error occurs138*/139@Override void close() throws IOException;140141/**142* Joins a multicast group to begin receiving all datagrams sent to the group,143* returning a membership key.144*145* <p> If this channel is currently a member of the group on the given146* interface to receive all datagrams then the membership key, representing147* that membership, is returned. Otherwise this channel joins the group and148* the resulting new membership key is returned. The resulting membership key149* is not {@link MembershipKey#sourceAddress source-specific}.150*151* <p> A multicast channel may join several multicast groups, including152* the same group on more than one interface. An implementation may impose a153* limit on the number of groups that may be joined at the same time.154*155* @param group156* The multicast address to join157* @param interf158* The network interface on which to join the group159*160* @return The membership key161*162* @throws IllegalArgumentException163* If the group parameter is not a {@link InetAddress#isMulticastAddress164* multicast} address, or the group parameter is an address type165* that is not supported by this channel166* @throws IllegalStateException167* If the channel already has source-specific membership of the168* group on the interface169* @throws UnsupportedOperationException170* If the channel's socket is not an Internet Protocol socket, or171* the platform does not support multicasting172* @throws ClosedChannelException173* If this channel is closed174* @throws IOException175* If an I/O error occurs176* @throws SecurityException177* If a security manager is set, and its178* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}179* method denies access to the multicast group180*/181MembershipKey join(InetAddress group, NetworkInterface interf)182throws IOException;183184/**185* Joins a multicast group to begin receiving datagrams sent to the group186* from a given source address.187*188* <p> If this channel is currently a member of the group on the given189* interface to receive datagrams from the given source address then the190* membership key, representing that membership, is returned. Otherwise this191* channel joins the group and the resulting new membership key is returned.192* The resulting membership key is {@link MembershipKey#sourceAddress193* source-specific}.194*195* <p> Membership is <em>cumulative</em> and this method may be invoked196* again with the same group and interface to allow receiving datagrams sent197* by other source addresses to the group.198*199* @param group200* The multicast address to join201* @param interf202* The network interface on which to join the group203* @param source204* The source address205*206* @return The membership key207*208* @throws IllegalArgumentException209* If the group parameter is not a {@link210* InetAddress#isMulticastAddress multicast} address, the211* source parameter is not a unicast address, the group212* parameter is an address type that is not supported by this channel,213* or the source parameter is not the same address type as the group214* @throws IllegalStateException215* If the channel is currently a member of the group on the given216* interface to receive all datagrams217* @throws UnsupportedOperationException218* If the channel's socket is not an Internet Protocol socket, or219* source filtering is not supported, or the platform does not220* support multicasting221* @throws ClosedChannelException222* If this channel is closed223* @throws IOException224* If an I/O error occurs225* @throws SecurityException226* If a security manager is set, and its227* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}228* method denies access to the multicast group229*/230MembershipKey join(InetAddress group, NetworkInterface interf, InetAddress source)231throws IOException;232}233234235