Package java.net
Class MulticastSocket
- java.lang.Object
-
- java.net.DatagramSocket
-
- java.net.MulticastSocket
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class MulticastSocket extends DatagramSocket
This class implements a multicast socket for sending and receiving IP multicast datagram packets.- See Also:
DatagramSocket
-
-
Constructor Summary
Constructors Constructor Description MulticastSocket()Constructs a multicast socket, bound to any available port on the local host.MulticastSocket(int port)Constructs a multicast socket, bound to the specifiedporton the local host.MulticastSocket(SocketAddress localAddress)Constructs aMulticastSocketbound to the address and port specified bylocalAddress, or an unboundMulticastSocketiflocalAddress == null.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description InetAddressgetInterface()Returns an address of the outgoing network interface used by this socket.booleangetLoopbackMode()Returns true if multicast loopback is disabled.NetworkInterfacegetNetworkInterface()Returns the outgoing network interface used by this socket.intgetTimeToLive()Returns the time-to-live (TTL) for multicast packets sent on this socket.bytegetTTL()Deprecated.UsegetTimeToLive()instead.voidjoinGroup(InetAddress groupAddr)Adds this socket to the specified multicast group.voidjoinGroup(SocketAddress groupAddress, NetworkInterface netInterface)Adds this socket to the specified multicast group.voidleaveGroup(InetAddress groupAddr)Removes this socket from the specified multicast group.voidleaveGroup(SocketAddress groupAddress, NetworkInterface netInterface)Removes this socket from the specified multicast group.voidsend(DatagramPacket packet, byte ttl)Deprecated.UsesetTimeToLive(int)instead.voidsetInterface(InetAddress address)Sets the outgoing network interface used by this socket.voidsetLoopbackMode(boolean disable)Disables multicast loopback ifdisable == true.voidsetNetworkInterface(NetworkInterface networkInterface)Sets the outgoing network interface used by this socket to the givennetworkInterface.voidsetTimeToLive(int ttl)Sets the time-to-live (TTL) for multicast packets sent on this socket.voidsetTTL(byte ttl)Deprecated.UsesetTimeToLive(int)instead.-
Methods inherited from class java.net.DatagramSocket
bind, close, connect, connect, disconnect, getBroadcast, getChannel, getFileDescriptor$, getInetAddress, getLocalAddress, getLocalPort, getLocalSocketAddress, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getReusePort, getSendBufferSize, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, onBind, onClose, onConnect, onDisconnect, receive, send, setBroadcast, setDatagramSocketImplFactory, setReceiveBufferSize, setReuseAddress, setReusePort, setSendBufferSize, setSoTimeout, setTrafficClass
-
-
-
-
Constructor Detail
-
MulticastSocket
public MulticastSocket() throws IOExceptionConstructs a multicast socket, bound to any available port on the local host.- Throws:
IOException- if an error occurs.
-
MulticastSocket
public MulticastSocket(int port) throws IOExceptionConstructs a multicast socket, bound to the specifiedporton the local host.- Throws:
IOException- if an error occurs.
-
MulticastSocket
public MulticastSocket(SocketAddress localAddress) throws IOException
Constructs aMulticastSocketbound to the address and port specified bylocalAddress, or an unboundMulticastSocketiflocalAddress == null.- Throws:
IllegalArgumentException- iflocalAddressis not supported (because it's not anInetSocketAddress, say).IOException- if an error occurs.
-
-
Method Detail
-
getInterface
public InetAddress getInterface() throws SocketException
Returns an address of the outgoing network interface used by this socket. To avoid inherent unpredictability, new code should usegetNetworkInterface()instead.- Throws:
SocketException- if an error occurs.
-
getNetworkInterface
public NetworkInterface getNetworkInterface() throws SocketException
Returns the outgoing network interface used by this socket.- Throws:
SocketException- if an error occurs.
-
getTimeToLive
public int getTimeToLive() throws IOExceptionReturns the time-to-live (TTL) for multicast packets sent on this socket.- Throws:
IOException- if an error occurs.
-
getTTL
@Deprecated public byte getTTL() throws IOException
Deprecated.UsegetTimeToLive()instead.Returns the time-to-live (TTL) for multicast packets sent on this socket.- Throws:
IOException- if an error occurs.
-
joinGroup
public void joinGroup(InetAddress groupAddr) throws IOException
Adds this socket to the specified multicast group. A socket must join a group before data may be received. A socket may be a member of multiple groups but may join any group only once.- Parameters:
groupAddr- the multicast group to be joined.- Throws:
IOException- if an error occurs.
-
joinGroup
public void joinGroup(SocketAddress groupAddress, NetworkInterface netInterface) throws IOException
Adds this socket to the specified multicast group. A socket must join a group before data may be received. A socket may be a member of multiple groups but may join any group only once.- Parameters:
groupAddress- the multicast group to be joined.netInterface- the network interface on which the datagram packets will be received.- Throws:
IOException- if the specified address is not a multicast address.IllegalArgumentException- if no multicast group is specified.
-
leaveGroup
public void leaveGroup(InetAddress groupAddr) throws IOException
Removes this socket from the specified multicast group.- Parameters:
groupAddr- the multicast group to be left.- Throws:
NullPointerException- ifgroupAddrisnull.IOException- if the specified group address is not a multicast address.
-
leaveGroup
public void leaveGroup(SocketAddress groupAddress, NetworkInterface netInterface) throws IOException
Removes this socket from the specified multicast group.- Parameters:
groupAddress- the multicast group to be left.netInterface- the network interface on which the addresses should be dropped.- Throws:
IOException- if the specified group address is not a multicast address.IllegalArgumentException- ifgroupAddressisnull.
-
send
@Deprecated public void send(DatagramPacket packet, byte ttl) throws IOException
Deprecated.UsesetTimeToLive(int)instead.Sends the givenpacketon this socket, using the giventtl. This method is deprecated because it modifies the TTL socket option for this socket twice on each call.- Throws:
IOException- if an error occurs.
-
setInterface
public void setInterface(InetAddress address) throws SocketException
Sets the outgoing network interface used by this socket. The interface used is the first interface found to have the givenaddress. To avoid inherent unpredictability, new code should usegetNetworkInterface()instead.- Throws:
SocketException- if an error occurs.
-
setNetworkInterface
public void setNetworkInterface(NetworkInterface networkInterface) throws SocketException
Sets the outgoing network interface used by this socket to the givennetworkInterface.- Overrides:
setNetworkInterfacein classDatagramSocket- Throws:
SocketException- if an error occurs.
-
setTimeToLive
public void setTimeToLive(int ttl) throws IOExceptionSets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.- Throws:
IOException- if an error occurs.
-
setTTL
@Deprecated public void setTTL(byte ttl) throws IOException
Deprecated.UsesetTimeToLive(int)instead.Sets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.- Throws:
IOException- if an error occurs.
-
getLoopbackMode
public boolean getLoopbackMode() throws SocketExceptionReturns true if multicast loopback is disabled. SeeSocketOptions.IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying UnixIP_MULTICAST_LOOP.- Throws:
SocketException- if an error occurs.
-
setLoopbackMode
public void setLoopbackMode(boolean disable) throws SocketExceptionDisables multicast loopback ifdisable == true. SeeSocketOptions.IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying UnixIP_MULTICAST_LOOP: true means disabled, false means enabled.- Throws:
SocketException- if an error occurs.
-
-