Home » Java programming language

Java SecurityManager checkMulticast() method with example

SecurityManager Class checkMulticast() method: Here, we are going to learn about the checkMulticast() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 16, 2019

SecurityManager Class checkMulticast() method

  • checkMulticast() method is available in java.lang package.
  • checkMulticast() method invokes SocketPermission(i_add.getHostAddress(),"accept,connect") permission to send, receive, join , leave IP multicast.
  • checkMulticast() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • checkMulticast() method may throw an exception at the time of determining IP address.
    • SecurityException – This exception may throw when the calling thread is not allowed to use send, receive, join and leave IP multicast.
    • NullPointerException – This exception may throw when the given argument value is null.

Syntax:

    public void checkMulticast(InetAddress i_add);

Parameter(s):

  • InetAddress i_add – represents the address of Internet Group Type.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void checkMulticast(InetAddress i_add)
// method of SecurityManager 

import java.net.*;

public class CheckMulticast extends SecurityManager {
    public static void main(String[] args) throws Exception {
        InetAddress i_add = InetAddress.getLocalHost();
        // By using setProperty() method is to set the policy property 
        // with security manager
        System.setProperty("java.security.policy", "file:/C:/java.policy");

        // Instantiating a CheckMulticast object
        CheckMulticast cm = new CheckMulticast();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(cm);

        // By using CheckMulticast(i_add) method is to check 
        // that ip address is accessible
        cm.checkMulticast(i_add);

        // Display the message
        System.out.println("Not Restricted..");
    }
}

Output

Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1" "connect,accept,resolve")
	at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
	at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
	at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
	at java.base/java.lang.SecurityManager.checkMulticast(SecurityManager.java:971)
	at CheckMulticast.main(CheckMulticast.java:23)



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.