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 localhost.
MulticastSocket(int aPort)
Constructs a multicast socket, bound to the specified port on the localhost.
MulticastSocket(SocketAddress localAddr)
Constructs a MulticastSocket bound to the host/port specified by the SocketAddress, or an unbound DatagramSocket if the SocketAddress is null.
Public Methods
InetAddress getInterface()
Gets the network address used by this socket.
boolean getLoopbackMode()
Gets the state of the SocketOptions.IP_MULTICAST_LOOP.
NetworkInterface getNetworkInterface()
Gets the network interface used by this socket.
byte getTTL()
This method is deprecated. Replaced by getTimeToLive()
int getTimeToLive()
Gets 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 pack, byte ttl)
This method is deprecated. use setTimeToLive(int).
void setInterface(InetAddress addr)
Sets the interface address used by this socket.
void setLoopbackMode(boolean disable)
void setNetworkInterface(NetworkInterface netInterface)
Sets the network interface used by this socket.
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 localhost.

Throws
IOException if an error occurs creating or binding the socket.

public MulticastSocket (int aPort)

Since: API Level 1

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

Parameters
aPort the port to bind on the localhost.
Throws
IOException if an error occurs creating or binding the socket.

public MulticastSocket (SocketAddress localAddr)

Since: API Level 1

Constructs a MulticastSocket bound to the host/port specified by the SocketAddress, or an unbound DatagramSocket if the SocketAddress is null.

Parameters
localAddr the local machine address and port to bind to.
Throws
IllegalArgumentException if the SocketAddress is not supported.
IOException if an error occurs creating or binding the socket.

Public Methods

public InetAddress getInterface ()

Since: API Level 1

Gets the network address used by this socket. This is useful on multihomed machines.

Returns
  • the address of the network interface through which the datagram packets are sent or received.
Throws
SocketException if an error occurs while getting the interface address.

public boolean getLoopbackMode ()

Since: API Level 1

Gets the state of the SocketOptions.IP_MULTICAST_LOOP.

Returns
  • true if the IP multicast loop is enabled, false otherwise.
Throws
SocketException if the socket is closed or the option is invalid.

public NetworkInterface getNetworkInterface ()

Since: API Level 1

Gets the network interface used by this socket. This is useful on multihomed machines.

Returns
  • the network interface used by this socket or null if no interface is set.
Throws
SocketException if an error occurs while getting the interface.

public byte getTTL ()

Since: API Level 1

This method is deprecated.
Replaced by getTimeToLive()

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

Returns
  • the default value for the time-to-life field.
Throws
IOException if an error occurs reading the default value.
See Also

public int getTimeToLive ()

Since: API Level 1

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

Returns
  • the default value for the time-to-life field.
Throws
IOException if an error occurs reading the default value.

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.
SecurityException if the caller is not authorized to join the group.
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 while joining a group.

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.
SecurityException if the caller is not authorized to leave the group.
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.
SecurityException if the caller is not authorized to leave the group.

public void send (DatagramPacket pack, byte ttl)

Since: API Level 1

This method is deprecated.
use setTimeToLive(int).

Send the packet on this socket. The packet must satisfy the security policy before it may be sent.

Parameters
pack the DatagramPacket to send
ttl the TTL setting for this transmission, overriding the socket default
Throws
IOException if an error occurs while sending data or setting options.

public void setInterface (InetAddress addr)

Since: API Level 1

Sets the interface address used by this socket. This allows to send multicast packets on a different interface than the default interface of the local system. This is useful on multihomed machines.

Parameters
addr the multicast interface network address to set.
Throws
SocketException if an error occurs while setting the network interface address option.

public void setLoopbackMode (boolean disable)

Since: API Level 1
Parameters
disable true to disable loopback
Throws
SocketException if the socket is closed or the option is invalid.

public void setNetworkInterface (NetworkInterface netInterface)

Since: API Level 1

Sets the network interface used by this socket. This is useful for multihomed machines.

Parameters
netInterface the multicast network interface to set.
Throws
SocketException if an error occurs while setting the network interface option.

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.

Parameters
ttl the default time-to-live field value for packets sent on this socket: 0 <= ttl <= 255.
Throws
IOException if an error occurs while setting the TTL option value.

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.

Parameters
ttl the default time-to-live field value for packets sent on this socket. 0 <= ttl <= 255.
Throws
IOException if an error occurs while setting the TTL option value.