Skip to content

Most visited

Recently visited

navigation
Added in API level 1

MulticastSocket

public class MulticastSocket
extends DatagramSocket

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


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 was deprecated in API level 1. Use getTimeToLive() instead.

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 was deprecated in API level 1. Use setTimeToLive(int) instead.

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 was deprecated in API level 1. Use setTimeToLive(int) instead.

void setTimeToLive(int ttl)

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

Inherited methods

From class java.net.DatagramSocket
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

MulticastSocket

Added in API level 1
MulticastSocket ()

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

Throws
IOException if an error occurs.

MulticastSocket

Added in API level 1
MulticastSocket (int port)

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

Parameters
port int
Throws
IOException if an error occurs.

MulticastSocket

Added in API level 1
MulticastSocket (SocketAddress localAddress)

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

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

Public methods

getInterface

Added in API level 1
InetAddress getInterface ()

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

Returns
InetAddress
Throws
SocketException if an error occurs.

getLoopbackMode

Added in API level 1
boolean getLoopbackMode ()

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.

Returns
boolean
Throws
SocketException if an error occurs.

getNetworkInterface

Added in API level 1
NetworkInterface getNetworkInterface ()

Returns the outgoing network interface used by this socket.

Returns
NetworkInterface
Throws
SocketException if an error occurs.

getTTL

Added in API level 1
byte getTTL ()

This method was deprecated in API level 1.
Use getTimeToLive() instead.

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

Returns
byte
Throws
IOException if an error occurs.

getTimeToLive

Added in API level 1
int getTimeToLive ()

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

Returns
int
Throws
IOException if an error occurs.

joinGroup

Added in API level 1
void joinGroup (SocketAddress groupAddress, 
                NetworkInterface netInterface)

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 SocketAddress: the multicast group to be joined.
netInterface NetworkInterface: 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.

joinGroup

Added in API level 1
void joinGroup (InetAddress groupAddr)

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 InetAddress: the multicast group to be joined.
Throws
IOException if an error occurs.

leaveGroup

Added in API level 1
void leaveGroup (SocketAddress groupAddress, 
                NetworkInterface netInterface)

Removes this socket from the specified multicast group.

Parameters
groupAddress SocketAddress: the multicast group to be left.
netInterface NetworkInterface: 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.

leaveGroup

Added in API level 1
void leaveGroup (InetAddress groupAddr)

Removes this socket from the specified multicast group.

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

send

Added in API level 1
void send (DatagramPacket packet, 
                byte ttl)

This method was deprecated in API level 1.
Use setTimeToLive(int) instead.

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.

Parameters
packet DatagramPacket
ttl byte
Throws
IOException if an error occurs.

setInterface

Added in API level 1
void setInterface (InetAddress address)

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.

Parameters
address InetAddress
Throws
SocketException if an error occurs.

setLoopbackMode

Added in API level 1
void setLoopbackMode (boolean disable)

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: true means disabled, false means enabled.

Parameters
disable boolean
Throws
SocketException if an error occurs.

setNetworkInterface

Added in API level 1
void setNetworkInterface (NetworkInterface networkInterface)

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

Parameters
networkInterface NetworkInterface
Throws
SocketException if an error occurs.

setTTL

Added in API level 1
void setTTL (byte ttl)

This method was deprecated in API level 1.
Use setTimeToLive(int) instead.

Sets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.

Parameters
ttl byte
Throws
IOException if an error occurs.

setTimeToLive

Added in API level 1
void setTimeToLive (int ttl)

Sets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.

Parameters
ttl int
Throws
IOException if an error occurs.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.