to top
Android APIs
public class

MulticastSocket

extends DatagramSocket
java.lang.Object
   ↳ java.net.DatagramSocket
     ↳ java.net.MulticastSocket

Class Overview

This class implements a multicast socket for sending and receiving IP multicast datagram packets.

See Also

Summary

Public Constructors
MulticastSocket()
Constructs a multicast socket, bound to any available port on the local host.
MulticastSocket(int port)
Constructs a multicast socket, bound to the specified port on the local host.
MulticastSocket(SocketAddress localAddress)
Constructs a MulticastSocket bound to the address and port specified by localAddress, or an unbound MulticastSocket if localAddress == null.
Public Methods
InetAddress getInterface()
Returns an address of the outgoing network interface used by this socket.
boolean getLoopbackMode()
Returns true if multicast loopback is disabled.
NetworkInterface getNetworkInterface()
Returns the outgoing network interface used by this socket.
byte getTTL()
This method is deprecated. Replaced by getTimeToLive()
int getTimeToLive()
Returns the time-to-live (TTL) for multicast packets sent on this socket.
void joinGroup(SocketAddress groupAddress, NetworkInterface netInterface)
Adds this socket to the specified multicast group.
void joinGroup(InetAddress groupAddr)
Adds this socket to the specified multicast group.
void leaveGroup(SocketAddress groupAddress, NetworkInterface netInterface)
Removes this socket from the specified multicast group.
void leaveGroup(InetAddress groupAddr)
Removes this socket from the specified multicast group.
void send(DatagramPacket packet, byte ttl)
This method is deprecated. use setTimeToLive(int).
void setInterface(InetAddress address)
Sets the outgoing network interface used by this socket.
void setLoopbackMode(boolean disable)
Disables multicast loopback if disable == true.
void setNetworkInterface(NetworkInterface networkInterface)
Sets the outgoing network interface used by this socket to the given networkInterface.
void setTTL(byte ttl)
This method is deprecated. Replaced by setTimeToLive(int)
void setTimeToLive(int ttl)
Sets the time-to-live (TTL) for multicast packets sent on this socket.
[Expand]
Inherited Methods
From class java.net.DatagramSocket
From class java.lang.Object

Public Constructors

public MulticastSocket ()

Since: API Level 1

Constructs a multicast socket, bound to any available port on the local host.

Throws
IOException if an error occurs.

public MulticastSocket (int port)

Since: API Level 1

Constructs a multicast socket, bound to the specified port on the local host.

Throws
IOException if an error occurs.

public MulticastSocket (SocketAddress localAddress)

Since: API Level 1

Constructs a MulticastSocket bound to the address and port specified by localAddress, or an unbound MulticastSocket if localAddress == null.

Throws
IllegalArgumentException if localAddress is not supported (because it's not an InetSocketAddress, say).
IOException if an error occurs.

Public Methods

public InetAddress getInterface ()

Since: API Level 1

Returns an address of the outgoing network interface used by this socket. To avoid inherent unpredictability, new code should use getNetworkInterface() instead.

Throws
SocketException if an error occurs.

public boolean getLoopbackMode ()

Since: API Level 1

Returns true if multicast loopback is disabled. See IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying Unix IP_MULTICAST_LOOP.

Throws
SocketException if an error occurs.

public NetworkInterface getNetworkInterface ()

Since: API Level 1

Returns the outgoing network interface used by this socket.

Throws
SocketException if an error occurs.

public byte getTTL ()

Since: API Level 1

This method is deprecated.
Replaced by getTimeToLive()

Returns the time-to-live (TTL) for multicast packets sent on this socket.

Throws
IOException if an error occurs.

public int getTimeToLive ()

Since: API Level 1

Returns the time-to-live (TTL) for multicast packets sent on this socket.

Throws
IOException if an error occurs.

public void joinGroup (SocketAddress groupAddress, NetworkInterface netInterface)

Since: API Level 1

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.

public void joinGroup (InetAddress groupAddr)

Since: API Level 1

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.

public void leaveGroup (SocketAddress groupAddress, NetworkInterface netInterface)

Since: API Level 1

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 if groupAddress is null.

public void leaveGroup (InetAddress groupAddr)

Since: API Level 1

Removes this socket from the specified multicast group.

Parameters
groupAddr the multicast group to be left.
Throws
NullPointerException if groupAddr is null.
IOException if the specified group address is not a multicast address.

public void send (DatagramPacket packet, byte ttl)

Since: API Level 1

This method is deprecated.
use setTimeToLive(int).

Sends the given packet on this socket, using the given ttl. This method is deprecated because it modifies the TTL socket option for this socket twice on each call.

Throws
IOException if an error occurs.

public void setInterface (InetAddress address)

Since: API Level 1

Sets the outgoing network interface used by this socket. The interface used is the first interface found to have the given address. To avoid inherent unpredictability, new code should use getNetworkInterface() instead.

Throws
SocketException if an error occurs.

public void setLoopbackMode (boolean disable)

Since: API Level 1

Disables multicast loopback if disable == true. See IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying Unix IP_MULTICAST_LOOP.

Throws
SocketException if an error occurs.

public void setNetworkInterface (NetworkInterface networkInterface)

Since: API Level 1

Sets the outgoing network interface used by this socket to the given networkInterface.

Throws
SocketException if an error occurs.

public void setTTL (byte ttl)

Since: API Level 1

This method is deprecated.
Replaced by setTimeToLive(int)

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.

public void setTimeToLive (int ttl)

Since: API Level 1

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.