📜  Java的.net.SocketPermission类在Java中

📅  最后修改于: 2022-05-13 01:54:41.042000             🧑  作者: Mango

Java的.net.SocketPermission类在Java中

Java.net.SocketPermisson 类表示您是否有权通过套接字访问网络。一个 SocketPermission 由一个主机和一组操作组成。

类声明:

public final class SocketPermission
extends Permission
implements Serializable

构造函数:

ConstructorDescription
ManagementPermission(String name)This constructs a ManagementPermission with the specified name.
ManagementPermission(String name, String actions)This constructs a new ManagementPermission object.

公共 SocketPermission(字符串主机,字符串操作):

用于创建具有指定操作的 SocketPermission 类的新对象。



方法:

MethodDescription
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.

例子:

Java
// Java Program to show the usage of
// java.net.SocketPermission Class
import 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");
    }
}


输出
connect,resolve
-1204607085
java.net.SocketPermissionCollection@30dae81 (
)