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
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.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, getSendBufferSize, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, receive, send, setBroadcast, setDatagramSocketImplFactory, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoTimeout, setTrafficClass
-
Constructor Details
-
MulticastSocket
Constructs a multicast socket, bound to any available port on the local host.- Throws:
IOException- if an error occurs.
-
MulticastSocket
Constructs a multicast socket, bound to the specifiedporton the local host.- Throws:
IOException- if an error occurs.
-
MulticastSocket
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 Details
-
getInterface
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
Returns the outgoing network interface used by this socket.- Throws:
SocketException- if an error occurs.
-
getTimeToLive
Returns the time-to-live (TTL) for multicast packets sent on this socket.- Throws:
IOException- if an error occurs.
-
getTTL
Deprecated.UsegetTimeToLive()instead.Returns the time-to-live (TTL) for multicast packets sent on this socket.- Throws:
IOException- if an error occurs.
-
joinGroup
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 IOExceptionAdds 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
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 IOExceptionRemoves 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.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
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
Sets the outgoing network interface used by this socket to the givennetworkInterface.- Overrides:
setNetworkInterfacein classDatagramSocket- Throws:
SocketException- if an error occurs.
-
setTimeToLive
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.
-
setTTL
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
Returns 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
Disables multicast loopback ifdisable == true. 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.
-
getTimeToLive()instead.