# MulticastSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/java/net/MulticastSocket#pubctors)\|[Methods](https://developer.android.com/reference/java/net/MulticastSocket#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/net/MulticastSocket#inhmethods)  

# MulticastSocket

`
public


class
MulticastSocket
`  
`


``

extends `[DatagramSocket](https://developer.android.com/reference/java/net/DatagramSocket)`


``


`

|---|---|--------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.net.DatagramSocket](https://developer.android.com/reference/java/net/DatagramSocket) ||
|   | ↳ | java.net.MulticastSocket |

<br />

*** ** * ** ***

The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet.

A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range`224.0.0.0`to`239.255.255.255`, inclusive. The address 224.0.0.0 is reserved and should not be used.

One would join a multicast group by first creating a MulticastSocket with the desired port, then invoking the`joinGroup(InetAddress groupAddr)`method:  

```
 // join a Multicast group and send the group salutations
 ...
 String msg = "Hello";
 InetAddress group = InetAddress.getByName("228.5.6.7");
 MulticastSocket s = new MulticastSocket(6789);
 s.joinGroup(group);
 byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
 DatagramPacket hi = new DatagramPacket(bytes, bytes.length,
                             group, 6789);
 s.send(hi);
 // get their responses!
 byte[] buf = new byte[1000];
 DatagramPacket recv = new DatagramPacket(buf, buf.length);
 s.receive(recv);
 ...
 // OK, I'm done talking - leave the group...
 s.leaveGroup(group);
 
```
When one sends a message to a multicast group,**all** subscribing recipients to that host and port receive the message (within the time-to-live range of the packet, see below). The socket needn't be a member of the multicast group to send messages to it.

When a socket subscribes to a multicast group/port, it receives datagrams sent by other hosts to the group/port, as do all other members of the group and port. A socket relinquishes membership in a group by the leaveGroup(InetAddress addr) method.**Multiple MulticastSocket's**may subscribe to a multicast group and port concurrently, and they will all receive group datagrams.

Currently applets are not allowed to use multicast sockets.

## Summary

|                                                                                                                                     ### Public constructors                                                                                                                                      ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[MulticastSocket](https://developer.android.com/reference/java/net/MulticastSocket#MulticastSocket())`() ` Create a multicast socket.                                                                                                                                                      |
| ` `[MulticastSocket](https://developer.android.com/reference/java/net/MulticastSocket#MulticastSocket(int))`(int port) ` Create a multicast socket and bind it to a specific port.                                                                                                            |
| ` `[MulticastSocket](https://developer.android.com/reference/java/net/MulticastSocket#MulticastSocket(java.net.SocketAddress))`(`[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)` bindaddr) ` Create a MulticastSocket bound to the specified socket address. |

|                                                                                                                                                                                                                                                                                                    ### Public methods                                                                                                                                                                                                                                                                                                     ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)                                                                        | ` `[getInterface](https://developer.android.com/reference/java/net/MulticastSocket#getInterface())`() ` Retrieve the address of the network interface used for multicast packets.                                                                                                                                                                                                                                                                                  |
| ` boolean`                                                                                                                                            | ` `[getLoopbackMode](https://developer.android.com/reference/java/net/MulticastSocket#getLoopbackMode())`() ` Get the setting for local loopback of multicast datagrams.                                                                                                                                                                                                                                                                                           |
| ` `[NetworkInterface](https://developer.android.com/reference/java/net/NetworkInterface)                                                              | ` `[getNetworkInterface](https://developer.android.com/reference/java/net/MulticastSocket#getNetworkInterface())`() ` Get the multicast network interface set.                                                                                                                                                                                                                                                                                                     |
| ` byte`                                                                                                                                               | ` `[getTTL](https://developer.android.com/reference/java/net/MulticastSocket#getTTL())`() ` *This method was deprecated in API level 15. use the getTimeToLive method instead, which returns an**int** instead of a**byte**.*                                                                                                                                                                                                                                      |
| ` int`                                                                                                                                                | ` `[getTimeToLive](https://developer.android.com/reference/java/net/MulticastSocket#getTimeToLive())`() ` Get the default time-to-live for multicast packets sent out on the socket.                                                                                                                                                                                                                                                                               |
| ` void`                                                                                                                                               | ` `[joinGroup](https://developer.android.com/reference/java/net/MulticastSocket#joinGroup(java.net.SocketAddress,%20java.net.NetworkInterface))`(`[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)` mcastaddr, `[NetworkInterface](https://developer.android.com/reference/java/net/NetworkInterface)` netIf) ` Joins the specified multicast group at the specified interface.                                                     |
| ` void`                                                                                                                                               | ` `[joinGroup](https://developer.android.com/reference/java/net/MulticastSocket#joinGroup(java.net.InetAddress))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` mcastaddr) ` Joins a multicast group.                                                                                                                                                                                                                              |
| ` void`                                                                                                                                               | ` `[leaveGroup](https://developer.android.com/reference/java/net/MulticastSocket#leaveGroup(java.net.SocketAddress,%20java.net.NetworkInterface))`(`[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)` mcastaddr, `[NetworkInterface](https://developer.android.com/reference/java/net/NetworkInterface)` netIf) ` Leave a multicast group on a specified local interface.                                                           |
| ` void`                                                                                                                                               | ` `[leaveGroup](https://developer.android.com/reference/java/net/MulticastSocket#leaveGroup(java.net.InetAddress))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` mcastaddr) ` Leave a multicast group.                                                                                                                                                                                                                            |
| ` void`                                                                                                                                               | ` `[send](https://developer.android.com/reference/java/net/MulticastSocket#send(java.net.DatagramPacket,%20byte))`(`[DatagramPacket](https://developer.android.com/reference/java/net/DatagramPacket)` p, byte ttl) ` *This method was deprecated in API level 15. Use the following code or its equivalent instead: ...... int ttl = mcastSocket.getTimeToLive(); mcastSocket.setTimeToLive(newttl); mcastSocket.send(p); mcastSocket.setTimeToLive(ttl); ......* |
| ` void`                                                                                                                                               | ` `[setInterface](https://developer.android.com/reference/java/net/MulticastSocket#setInterface(java.net.InetAddress))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` inf) ` Set the multicast network interface used by methods whose behavior would be affected by the value of the network interface.                                                                                                                           |
| ` void`                                                                                                                                               | ` `[setLoopbackMode](https://developer.android.com/reference/java/net/MulticastSocket#setLoopbackMode(boolean))`(boolean disable) ` Disable/Enable local loopback of multicast datagrams The option is used by the platform's networking code as a hint for setting whether multicast data will be looped back to the local socket.                                                                                                                                |
| ` void`                                                                                                                                               | ` `[setNetworkInterface](https://developer.android.com/reference/java/net/MulticastSocket#setNetworkInterface(java.net.NetworkInterface))`(`[NetworkInterface](https://developer.android.com/reference/java/net/NetworkInterface)` netIf) ` Specify the network interface for outgoing multicast datagrams sent on this socket.                                                                                                                                    |
| ` void`                                                                                                                                               | ` `[setTTL](https://developer.android.com/reference/java/net/MulticastSocket#setTTL(byte))`(byte ttl) ` *This method was deprecated in API level 15. use the setTimeToLive method instead, which uses**int** instead of**byte**as the type for ttl.*                                                                                                                                                                                                               |
| ` void`                                                                                                                                               | ` `[setTimeToLive](https://developer.android.com/reference/java/net/MulticastSocket#setTimeToLive(int))`(int ttl) ` Set the default time-to-live for multicast packets sent out on this`MulticastSocket`in order to control the scope of the multicasts.                                                                                                                                                                                                           |
| ` `[Set](https://developer.android.com/reference/java/util/Set)`<`[SocketOption](https://developer.android.com/reference/java/net/SocketOption)`<?>>` | ` `[supportedOptions](https://developer.android.com/reference/java/net/MulticastSocket#supportedOptions())`() ` Returns a set of the socket options supported by this socket.                                                                                                                                                                                                                                                                                      |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.net.DatagramSocket](https://developer.android.com/reference/java/net/DatagramSocket)` ` |-------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void`                                                                                                                                               | ` `[bind](https://developer.android.com/reference/java/net/DatagramSocket#bind(java.net.SocketAddress))`(`[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)` addr) ` Binds this DatagramSocket to a specific address and port.                                                                                               | | ` void`                                                                                                                                               | ` `[close](https://developer.android.com/reference/java/net/DatagramSocket#close())`() ` Closes this datagram socket.                                                                                                                                                                                                                                      | | ` void`                                                                                                                                               | ` `[connect](https://developer.android.com/reference/java/net/DatagramSocket#connect(java.net.InetAddress,%20int))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` address, int port) ` Connects the socket to a remote address for this socket.                                                                            | | ` void`                                                                                                                                               | ` `[connect](https://developer.android.com/reference/java/net/DatagramSocket#connect(java.net.SocketAddress))`(`[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)` addr) ` Connects this socket to a remote socket address (IP address + port number).                                                                       | | ` void`                                                                                                                                               | ` `[disconnect](https://developer.android.com/reference/java/net/DatagramSocket#disconnect())`() ` Disconnects the socket.                                                                                                                                                                                                                                 | | ` boolean`                                                                                                                                            | ` `[getBroadcast](https://developer.android.com/reference/java/net/DatagramSocket#getBroadcast())`() ` Tests if SO_BROADCAST is enabled.                                                                                                                                                                                                                   | | ` `[DatagramChannel](https://developer.android.com/reference/java/nio/channels/DatagramChannel)                                                       | ` `[getChannel](https://developer.android.com/reference/java/net/DatagramSocket#getChannel())`() ` Returns the unique[DatagramChannel](https://developer.android.com/reference/java/nio/channels/DatagramChannel)object associated with this datagram socket, if any.                                                                                      | | ` `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)                                                                        | ` `[getInetAddress](https://developer.android.com/reference/java/net/DatagramSocket#getInetAddress())`() ` Returns the address to which this socket is connected.                                                                                                                                                                                          | | ` `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)                                                                        | ` `[getLocalAddress](https://developer.android.com/reference/java/net/DatagramSocket#getLocalAddress())`() ` Gets the local address to which the socket is bound.                                                                                                                                                                                          | | ` int`                                                                                                                                                | ` `[getLocalPort](https://developer.android.com/reference/java/net/DatagramSocket#getLocalPort())`() ` Returns the port number on the local host to which this socket is bound.                                                                                                                                                                            | | ` `[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)                                                                    | ` `[getLocalSocketAddress](https://developer.android.com/reference/java/net/DatagramSocket#getLocalSocketAddress())`() ` Returns the address of the endpoint this socket is bound to.                                                                                                                                                                      | | ` <T> T`                                                                                                                                              | ` `[getOption](https://developer.android.com/reference/java/net/DatagramSocket#getOption(java.net.SocketOption<T>))`(`[SocketOption](https://developer.android.com/reference/java/net/SocketOption)`<T> name) ` Returns the value of a socket option.                                                                                                      | | ` int`                                                                                                                                                | ` `[getPort](https://developer.android.com/reference/java/net/DatagramSocket#getPort())`() ` Returns the port number to which this socket is connected.                                                                                                                                                                                                    | | ` int`                                                                                                                                                | ` `[getReceiveBufferSize](https://developer.android.com/reference/java/net/DatagramSocket#getReceiveBufferSize())`() ` Get value of the SO_RCVBUF option for this`DatagramSocket`, that is the buffer size used by the platform for input on this`DatagramSocket`.                                                                                         | | ` `[SocketAddress](https://developer.android.com/reference/java/net/SocketAddress)                                                                    | ` `[getRemoteSocketAddress](https://developer.android.com/reference/java/net/DatagramSocket#getRemoteSocketAddress())`() ` Returns the address of the endpoint this socket is connected to, or`null`if it is unconnected.                                                                                                                                  | | ` boolean`                                                                                                                                            | ` `[getReuseAddress](https://developer.android.com/reference/java/net/DatagramSocket#getReuseAddress())`() ` Tests if SO_REUSEADDR is enabled.                                                                                                                                                                                                             | | ` int`                                                                                                                                                | ` `[getSendBufferSize](https://developer.android.com/reference/java/net/DatagramSocket#getSendBufferSize())`() ` Get value of the SO_SNDBUF option for this`DatagramSocket`, that is the buffer size used by the platform for output on this`DatagramSocket`.                                                                                              | | ` int`                                                                                                                                                | ` `[getSoTimeout](https://developer.android.com/reference/java/net/DatagramSocket#getSoTimeout())`() ` Retrieve setting for SO_TIMEOUT.                                                                                                                                                                                                                    | | ` int`                                                                                                                                                | ` `[getTrafficClass](https://developer.android.com/reference/java/net/DatagramSocket#getTrafficClass())`() ` Gets traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.                                                                                                                                    | | ` boolean`                                                                                                                                            | ` `[isBound](https://developer.android.com/reference/java/net/DatagramSocket#isBound())`() ` Returns the binding state of the socket.                                                                                                                                                                                                                      | | ` boolean`                                                                                                                                            | ` `[isClosed](https://developer.android.com/reference/java/net/DatagramSocket#isClosed())`() ` Returns whether the socket is closed or not.                                                                                                                                                                                                                | | ` boolean`                                                                                                                                            | ` `[isConnected](https://developer.android.com/reference/java/net/DatagramSocket#isConnected())`() ` Returns the connection state of the socket.                                                                                                                                                                                                           | | ` void`                                                                                                                                               | ` `[receive](https://developer.android.com/reference/java/net/DatagramSocket#receive(java.net.DatagramPacket))`(`[DatagramPacket](https://developer.android.com/reference/java/net/DatagramPacket)` p) ` Receives a datagram packet from this socket.                                                                                                      | | ` void`                                                                                                                                               | ` `[send](https://developer.android.com/reference/java/net/DatagramSocket#send(java.net.DatagramPacket))`(`[DatagramPacket](https://developer.android.com/reference/java/net/DatagramPacket)` p) ` Sends a datagram packet from this socket.                                                                                                               | | ` void`                                                                                                                                               | ` `[setBroadcast](https://developer.android.com/reference/java/net/DatagramSocket#setBroadcast(boolean))`(boolean on) ` Enable/disable SO_BROADCAST.                                                                                                                                                                                                       | | ` static void`                                                                                                                                        | ` `[setDatagramSocketImplFactory](https://developer.android.com/reference/java/net/DatagramSocket#setDatagramSocketImplFactory(java.net.DatagramSocketImplFactory))`(`[DatagramSocketImplFactory](https://developer.android.com/reference/java/net/DatagramSocketImplFactory)` fac) ` Sets the datagram socket implementation factory for the application. | | ` <T> `[DatagramSocket](https://developer.android.com/reference/java/net/DatagramSocket)                                                              | ` `[setOption](https://developer.android.com/reference/java/net/DatagramSocket#setOption(java.net.SocketOption<T>,%20T))`(`[SocketOption](https://developer.android.com/reference/java/net/SocketOption)`<T> name, T value) ` Sets the value of a socket option.                                                                                           | | ` void`                                                                                                                                               | ` `[setReceiveBufferSize](https://developer.android.com/reference/java/net/DatagramSocket#setReceiveBufferSize(int))`(int size) ` Sets the SO_RCVBUF option to the specified value for this`DatagramSocket`.                                                                                                                                               | | ` void`                                                                                                                                               | ` `[setReuseAddress](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))`(boolean on) ` Enable/disable the SO_REUSEADDR socket option.                                                                                                                                                                               | | ` void`                                                                                                                                               | ` `[setSendBufferSize](https://developer.android.com/reference/java/net/DatagramSocket#setSendBufferSize(int))`(int size) ` Sets the SO_SNDBUF option to the specified value for this`DatagramSocket`.                                                                                                                                                     | | ` void`                                                                                                                                               | ` `[setSoTimeout](https://developer.android.com/reference/java/net/DatagramSocket#setSoTimeout(int))`(int timeout) ` Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.                                                                                                                                                                | | ` void`                                                                                                                                               | ` `[setTrafficClass](https://developer.android.com/reference/java/net/DatagramSocket#setTrafficClass(int))`(int tc) ` Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.                                                                                                                   | | ` `[Set](https://developer.android.com/reference/java/util/Set)`<`[SocketOption](https://developer.android.com/reference/java/net/SocketOption)`<?>>` | ` `[supportedOptions](https://developer.android.com/reference/java/net/DatagramSocket#supportedOptions())`() ` Returns a set of the socket options supported by this socket.                                                                                                                                                                               | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public constructors

### MulticastSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MulticastSocket ()
```

Create a multicast socket.

If there is a security manager, its`checkListen`method is first called with 0 as its argument to ensure the operation is allowed. This could result in a SecurityException.

When the socket is created the[DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))method is called to enable the SO_REUSEADDR socket option.

<br />

|                                                                                     Throws                                                                                     ||
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | if an I/O exception occurs while creating the MulticastSocket                        |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkListen`method doesn't allow the operation. |

**See also:**

- [SecurityManager.checkListen(int)](https://developer.android.com/reference/java/lang/SecurityManager#checkListen(int))
- [DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))
- [DatagramSocketImpl.setOption(SocketOption, Object)](https://developer.android.com/reference/java/net/DatagramSocketImpl#setOption(java.net.SocketOption<T>,%20T))  

### MulticastSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MulticastSocket (int port)
```

Create a multicast socket and bind it to a specific port.

If there is a security manager, its`checkListen`method is first called with the`port`argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

When the socket is created the[DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))method is called to enable the SO_REUSEADDR socket option.

<br />

|            Parameters             ||
|--------|---------------------------|
| `port` | `int`: port to use <br /> |

|                                                                                     Throws                                                                                     ||
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | if an I/O exception occurs while creating the MulticastSocket                        |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkListen`method doesn't allow the operation. |

**See also:**

- [SecurityManager.checkListen(int)](https://developer.android.com/reference/java/lang/SecurityManager#checkListen(int))
- [DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))  

### MulticastSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MulticastSocket (SocketAddress bindaddr)
```

Create a MulticastSocket bound to the specified socket address.

Or, if the address is`null`, create an unbound socket.

If there is a security manager, its`checkListen`method is first called with the SocketAddress port as its argument to ensure the operation is allowed. This could result in a SecurityException.

When the socket is created the[DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))method is called to enable the SO_REUSEADDR socket option.

<br />

|                                          Parameters                                           ||
|------------|-----------------------------------------------------------------------------------|
| `bindaddr` | `SocketAddress`: Socket address to bind to, or`null`for an unbound socket. <br /> |

|                                                                                     Throws                                                                                     ||
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | if an I/O exception occurs while creating the MulticastSocket                        |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkListen`method doesn't allow the operation. |

**See also:**

- [SecurityManager.checkListen(int)](https://developer.android.com/reference/java/lang/SecurityManager#checkListen(int))
- [DatagramSocket.setReuseAddress(boolean)](https://developer.android.com/reference/java/net/DatagramSocket#setReuseAddress(boolean))

## Public methods

### getInterface

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public InetAddress getInterface ()
```

Retrieve the address of the network interface used for multicast packets.

<br />

|                                                                                     Returns                                                                                      ||
|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
| [InetAddress](https://developer.android.com/reference/java/net/InetAddress) | An`InetAddress`representing the address of the network interface used for multicast packets. <br /> |

|                                                                           Throws                                                                           ||
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if there is an error in the underlying protocol, such as a TCP error. |

**See also:**

- [setInterface(java.net.InetAddress)](https://developer.android.com/reference/java/net/MulticastSocket#setInterface(java.net.InetAddress))  

### getLoopbackMode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean getLoopbackMode ()
```

Get the setting for local loopback of multicast datagrams.

<br />

|                           Returns                            ||
|-----------|---------------------------------------------------|
| `boolean` | true if the LoopbackMode has been disabled <br /> |

|                                                             Throws                                                              ||
|-------------------------------------------------------------------------------------|--------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if an error occurs while getting the value |

**See also:**

- [setLoopbackMode(boolean)](https://developer.android.com/reference/java/net/MulticastSocket#setLoopbackMode(boolean))  

### getNetworkInterface

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public NetworkInterface getNetworkInterface ()
```

Get the multicast network interface set.

<br />

|                                                                                   Returns                                                                                    ||
|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| [NetworkInterface](https://developer.android.com/reference/java/net/NetworkInterface) | the multicast`NetworkInterface`currently set or`null`when no interface is set. <br /> |

|                                                                           Throws                                                                           ||
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if there is an error in the underlying protocol, such as a TCP error. |

**See also:**

- [setNetworkInterface(NetworkInterface)](https://developer.android.com/reference/java/net/MulticastSocket#setNetworkInterface(java.net.NetworkInterface))  

### getTTL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public byte getTTL ()
```

<br />

**This method was deprecated in API level 15.**   
use the getTimeToLive method instead, which returns an**int** instead of a**byte**.

Get the default time-to-live for multicast packets sent out on the socket.

<br />

|                    Returns                    ||
|--------|---------------------------------------|
| `byte` | the default time-to-live value <br /> |

|                                                                       Throws                                                                        ||
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O exception occurs while getting the default time-to-live value |

**See also:**

- [setTTL(byte)](https://developer.android.com/reference/java/net/MulticastSocket#setTTL(byte))  

### getTimeToLive

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getTimeToLive ()
```

Get the default time-to-live for multicast packets sent out on the socket.

<br />

|                   Returns                    ||
|-------|---------------------------------------|
| `int` | the default time-to-live value <br /> |

|                                                                       Throws                                                                        ||
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O exception occurs while getting the default time-to-live value |

**See also:**

- [setTimeToLive(int)](https://developer.android.com/reference/java/net/MulticastSocket#setTimeToLive(int))  

### joinGroup

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void joinGroup (SocketAddress mcastaddr, 
                NetworkInterface netIf)
```

Joins the specified multicast group at the specified interface.

If there is a security manager, this method first calls its`checkMulticast`method with the`mcastaddr`argument as its argument.

<br />

|                                                                                                                                                                                                                                    Parameters                                                                                                                                                                                                                                    ||
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mcastaddr` | `SocketAddress`: is the multicast address to join <br />                                                                                                                                                                                                                                                                                                                                                                                                            |
| `netIf`     | `NetworkInterface`: specifies the local interface to receive multicast datagram packets, or*null* to defer to the interface set by[MulticastSocket.setInterface(InetAddress)](https://developer.android.com/reference/java/net/MulticastSocket#setInterface(java.net.InetAddress))or[MulticastSocket.setNetworkInterface(NetworkInterface)](https://developer.android.com/reference/java/net/MulticastSocket#setNetworkInterface(java.net.NetworkInterface)) <br /> |

|                                                                                                               Throws                                                                                                                ||
|--------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if there is an error joining, or when the address is not a multicast address, or the platform does not support multicasting |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkMulticast`method doesn't allow the join.                                          |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if mcastaddr is null or is a SocketAddress subclass not supported by this socket                                            |

**See also:**

- [SecurityManager.checkMulticast(InetAddress)](https://developer.android.com/reference/java/lang/SecurityManager#checkMulticast(java.net.InetAddress))  

### joinGroup

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void joinGroup (InetAddress mcastaddr)
```

Joins a multicast group. Its behavior may be affected by`setInterface`or`setNetworkInterface`.

If there is a security manager, this method first calls its`checkMulticast`method with the`mcastaddr`argument as its argument.

<br />

|                             Parameters                              ||
|-------------|--------------------------------------------------------|
| `mcastaddr` | `InetAddress`: is the multicast address to join <br /> |

|                                                                                                        Throws                                                                                                         ||
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | if there is an error joining, or when the address is not a multicast address, or the platform does not support multicasting |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkMulticast`method doesn't allow the join.                                          |

**See also:**

- [SecurityManager.checkMulticast(InetAddress)](https://developer.android.com/reference/java/lang/SecurityManager#checkMulticast(java.net.InetAddress))  

### leaveGroup

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void leaveGroup (SocketAddress mcastaddr, 
                NetworkInterface netIf)
```

Leave a multicast group on a specified local interface.

If there is a security manager, this method first calls its`checkMulticast`method with the`mcastaddr`argument as its argument.

<br />

|                                                                                                                                                                                                                Parameters                                                                                                                                                                                                                 ||
|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mcastaddr` | `SocketAddress`: is the multicast address to leave <br />                                                                                                                                                                                                                                                                                                                                                                    |
| `netIf`     | `NetworkInterface`: specifies the local interface or*null* to defer to the interface set by[MulticastSocket.setInterface(InetAddress)](https://developer.android.com/reference/java/net/MulticastSocket#setInterface(java.net.InetAddress))or[MulticastSocket.setNetworkInterface(NetworkInterface)](https://developer.android.com/reference/java/net/MulticastSocket#setNetworkInterface(java.net.NetworkInterface)) <br /> |

|                                                                                             Throws                                                                                              ||
|--------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if there is an error leaving or when the address is not a multicast address.            |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkMulticast`method doesn't allow the operation. |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if mcastaddr is null or is a SocketAddress subclass not supported by this socket        |

**See also:**

- [SecurityManager.checkMulticast(InetAddress)](https://developer.android.com/reference/java/lang/SecurityManager#checkMulticast(java.net.InetAddress))  

### leaveGroup

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void leaveGroup (InetAddress mcastaddr)
```

Leave a multicast group. Its behavior may be affected by`setInterface`or`setNetworkInterface`.

If there is a security manager, this method first calls its`checkMulticast`method with the`mcastaddr`argument as its argument.

<br />

|                              Parameters                              ||
|-------------|---------------------------------------------------------|
| `mcastaddr` | `InetAddress`: is the multicast address to leave <br /> |

|                                                                                      Throws                                                                                       ||
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | if there is an error leaving or when the address is not a multicast address.            |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkMulticast`method doesn't allow the operation. |

**See also:**

- [SecurityManager.checkMulticast(InetAddress)](https://developer.android.com/reference/java/lang/SecurityManager#checkMulticast(java.net.InetAddress))  

### send

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void send (DatagramPacket p, 
                byte ttl)
```

<br />

**This method was deprecated in API level 15.**   
Use the following code or its equivalent instead: ...... int ttl = mcastSocket.getTimeToLive(); mcastSocket.setTimeToLive(newttl); mcastSocket.send(p); mcastSocket.setTimeToLive(ttl); ......

Sends a datagram packet to the destination, with a TTL (time- to-live) other than the default for the socket. This method need only be used in instances where a particular TTL is desired; otherwise it is preferable to set a TTL once on the socket, and use that default TTL for all packets. This method does**not** alter the default TTL for the socket. Its behavior may be affected by`setInterface`.

If there is a security manager, this method first performs some security checks. First, if`p.getAddress().isMulticastAddress()`is true, this method calls the security manager's`checkMulticast`method with`p.getAddress()`and`ttl`as its arguments. If the evaluation of that expression is false, this method instead calls the security manager's`checkConnect`method with arguments`p.getAddress().getHostAddress()`and`p.getPort()`. Each call to a security manager method could result in a SecurityException if the operation is not allowed.

<br />

|                                                                                                                      Parameters                                                                                                                       ||
|-------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `p`   | `DatagramPacket`: is the packet to be sent. The packet should contain the destination multicast ip address and the data to be sent. One does not need to be the member of the group to send packets to a destination multicast address. <br /> |
| `ttl` | `byte`: optional time to live for multicast packet. default ttl is 1. <br />                                                                                                                                                                   |

|                                                                                            Throws                                                                                            ||
|------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)               | is raised if an error occurs i.e error while setting ttl.                                          |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException) | if a security manager exists and its`checkMulticast`or`checkConnect`method doesn't allow the send. |

**See also:**

- [DatagramSocket.send(DatagramPacket)](https://developer.android.com/reference/java/net/DatagramSocket#send(java.net.DatagramPacket))
- [DatagramSocket.receive(DatagramPacket)](https://developer.android.com/reference/java/net/DatagramSocket#receive(java.net.DatagramPacket))
- [SecurityManager.checkMulticast(java.net.InetAddress, byte)](https://developer.android.com/reference/java/lang/SecurityManager#checkMulticast(java.net.InetAddress,%20byte))
- [SecurityManager.checkConnect(String, int)](https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int))  

### setInterface

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setInterface (InetAddress inf)
```

Set the multicast network interface used by methods whose behavior would be affected by the value of the network interface. Useful for multihomed hosts.

<br />

|                  Parameters                  ||
|-------|---------------------------------------|
| `inf` | `InetAddress`: the InetAddress <br /> |

|                                                                           Throws                                                                           ||
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if there is an error in the underlying protocol, such as a TCP error. |

**See also:**

- [getInterface()](https://developer.android.com/reference/java/net/MulticastSocket#getInterface())  

### setLoopbackMode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setLoopbackMode (boolean disable)
```

Disable/Enable local loopback of multicast datagrams The option is used by the platform's networking code as a hint for setting whether multicast data will be looped back to the local socket.

Because this option is a hint, applications that want to verify what loopback mode is set to should call[getLoopbackMode()](https://developer.android.com/reference/java/net/MulticastSocket#getLoopbackMode())

<br />

|                          Parameters                           ||
|-----------|----------------------------------------------------|
| `disable` | `boolean`:`true`to disable the LoopbackMode <br /> |

|                                                             Throws                                                              ||
|-------------------------------------------------------------------------------------|--------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if an error occurs while setting the value |

**See also:**

- [getLoopbackMode()](https://developer.android.com/reference/java/net/MulticastSocket#getLoopbackMode())  

### setNetworkInterface

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setNetworkInterface (NetworkInterface netIf)
```

Specify the network interface for outgoing multicast datagrams sent on this socket.

<br />

|                    Parameters                     ||
|---------|------------------------------------------|
| `netIf` | `NetworkInterface`: the interface <br /> |

|                                                                           Throws                                                                           ||
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [SocketException](https://developer.android.com/reference/java/net/SocketException) | if there is an error in the underlying protocol, such as a TCP error. |

**See also:**

- [getNetworkInterface()](https://developer.android.com/reference/java/net/MulticastSocket#getNetworkInterface())  

### setTTL

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setTTL (byte ttl)
```

<br />

**This method was deprecated in API level 15.**   
use the setTimeToLive method instead, which uses**int** instead of**byte**as the type for ttl.

Set the default time-to-live for multicast packets sent out on this`MulticastSocket`in order to control the scope of the multicasts.

The ttl is an**unsigned** 8-bit quantity, and so**must** be in the range`0 <= ttl <= 0xFF `.

<br />

|               Parameters               ||
|-------|---------------------------------|
| `ttl` | `byte`: the time-to-live <br /> |

|                                                                       Throws                                                                        ||
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O exception occurs while setting the default time-to-live value |

**See also:**

- [getTTL()](https://developer.android.com/reference/java/net/MulticastSocket#getTTL())  

### setTimeToLive

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void setTimeToLive (int ttl)
```

Set the default time-to-live for multicast packets sent out on this`MulticastSocket`in order to control the scope of the multicasts.

The ttl**must** be in the range`0 <= ttl <=
255`or an`IllegalArgumentException`will be thrown. Multicast packets sent with a TTL of`0`are not transmitted on the network but may be delivered locally.

<br />

|              Parameters               ||
|-------|--------------------------------|
| `ttl` | `int`: the time-to-live <br /> |

|                                                                       Throws                                                                        ||
|----------------------------------------------------------------------------|-------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O exception occurs while setting the default time-to-live value |

**See also:**

- [getTimeToLive()](https://developer.android.com/reference/java/net/MulticastSocket#getTimeToLive())  

### supportedOptions

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Set<SocketOption<?>> supportedOptions ()
```

Returns a set of the socket options supported by this socket. This method will continue to return the set of options even after the socket has been closed.

<br />

|                                                                                                                                           Returns                                                                                                                                            ||
|----------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| [Set](https://developer.android.com/reference/java/util/Set)`<`[SocketOption](https://developer.android.com/reference/java/net/SocketOption)`<?>>` | A set of the socket options supported by this socket. This set may be empty if the socket's DatagramSocketImpl cannot be created. <br /> |