java.net.SocketPermission Class in Java

The java.net.SocketPermisson class represents whether you have permission to access a network via sockets. A SocketPermission consists of a host and a set of actions.
Class Declaration:
public final class SocketPermission extends Permission implements Serializable
Constructor:
| Constructor | Description |
|---|---|
| ManagementPermission(String name) | This constructs a ManagementPermission with the specified name. |
| ManagementPermission(String name, String actions) | This constructs a new ManagementPermission object. |
public SocketPermission(String host, String action):Â
Used for creating a new object of SocketPermission class with specified actions.
Methods:
| Method | Description |
|---|---|
| equals(object obj) | It checks whether the two SocketPermission objects are equal or not. |
| getActions() | It returns the actions of this SocketPermission object in String format |
| hashcode() | It returns the hash code value for this object. |
| implies(Permission p) | It checks whether this SocketPermssion object implies this permission or not. |
| newPermissonCollection() | It returns a new PermissionCollection object. |
Example:
Java
// Java Program to show the usage of// java.net.SocketPermission Classimport java.io.IOException;import java.net.SocketPermission;import java.security.Permission;import java.security.PermissionCollection;Â
public class Socket {Â
    public static void main(String args[])    {        try {            // getting permission object            Permission p = getPermission();Â
            // print actions of permission p            System.out.println(p.getActions());Â
            // printing hashcode value of permission p            System.out.println(p.hashCode());Â
            // creating a permissionCollection object and            // printing it            PermissionCollection p1                = p.newPermissionCollection();            System.out.print(p1);        }        catch (Exception e) {            System.err.print("Permission denied");        }    }    public static Permission getPermission()        throws IOException    {        int port = 3000;        String host = "localhost";        return new SocketPermission(host + ":" + port,                                    "Connect,resolve");    }} |
Output
connect,resolve -1204607085 java.net.SocketPermissionCollection@30dae81 ( )
Â



