MulticastSocket leaveGroup(SocketAddress, NetworkInterface) in Java

leaveGroup(SocketAddress, NetworkInterface): This method is available in the java.net.MulticastSocket class of Java.

Syntax:

void java.net.MulticastSocket.leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) throws IOException

This method takes two arguments. This method leaves 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.

Parameters: Two parameters are required for this method.

mcastaddr: is the multicast address to leave.

netIf: specifies the local interface or null to defer to the interface set by MulticastSocket.setInterface(InetAddress) or MulticastSocket.setNetworkInterface(NetworkInterface).If null, and no interface has been set, the behavior is unspecified: any interface may be selected or the operation may fail with a SocketException.

Throws:

1. IOException - if an error is leaving or when the address is not multicast.

2. SecurityException - if a security manager exists and its checkMulticast method doesn't allow the operation.

3. IllegalArgumentException - if mcastaddr is null or is aSocketAddress subclass not supported by this socket.

Approach

Java

package com.MulticastSocket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketAddress;

public class MulticastSocketleaveGroup {
    public static void main(String[] args) throws IOException {

        MulticastSocket multicastSocket = new MulticastSocket();

        int port = 8080;
        SocketAddress mcastaddr = new InetSocketAddress(port);
        int index = 1;
        NetworkInterface netif = NetworkInterface.getByIndex(index);
        multicastSocket.leaveGroup(mcastaddr, netif);
        System.out.println("Successfully leave group");
    }
}

Output:

Exception in thread "main" java.net.SocketException: Not a multicast address at java.base/sun.nio.ch.DatagramSocketAdaptor.checkGroup(DatagramSocketAdaptor.java:513) at java.base/sun.nio.ch.DatagramSocketAdaptor.leaveGroup(DatagramSocketAdaptor.java:538) at java.base/java.net.MulticastSocket.leaveGroup(MulticastSocket.java:413) at com.MulticastSocket.MulticastSocketleaveGroup.main(MulticastSocketleaveGroup.java:18)


Some other methods of MulticastSocket class

MulticastSocket()This method constructs a multicast socket and binds it to any available port on the local host machine. 

MulticastSocket(int)This method constructs a multicast socket and binds it to the specified port on the local host machine.

MulticastSocket(SocketAddress)This method creates a multicast socket, bound to the specified local socket address.

getNetworkInterface()This method gets the multicast network interface set.

getTimeToLive()This method gets the default time-to-live for multicast packets sent out on the socket.

joinGroup(SocketAddress, NetworkInterface)This method joins the specified multicast group at the specified interface.

leaveGroup(SocketAddress, NetworkInterface)This method leaves a multicast group on a specified local interface.

setNetworkInterface(NetworkInterface)This method specifies the network interface for outgoing multicast datagrams sent on this socket.

setTimeToLive(int)This method sets the default time-to-live for multicast packets sent out to this MulticastSocket in order to control the scope of the multicast.

No comments:

Post a Comment