dcsimg
 

How to Provide Permission to Sockets in Java

Thursday May 20th 2021 by MS Sridhar
Article Image

In this quick Java guide, you can follow along as we learn how to provide permission to Sockets using Java.

Java has a built-in class called SocketPermission, which gives us the ability to provide permissions to a Socket. You can use different connection methods to connect to a host, including accept, connect, listen, and resolve.

Note that resolve gets added to every Socket in which one of the other three methods are provided. Here is an example of how to provide permissions to a Socket in Java:

*/

import java.net.SocketPermission;

class SocketPermissionEx
{
	public static void main(String[] args)
	{
		SocketPermissionEx socketPermissionEx = new SocketPermissionEx();
		socketPermissionEx.proceed();
	}

	private void proceed()
	{
		SocketPermission socketPermission = new SocketPermission("localhost:6581", "connect");
		System.out.println("Socket permission: " + socketPermission);
	}
}

/*

Output:

$java SocketPermissionEx
Socket permission: ("java.net.SocketPermission" "localhost:6581" "connect,resolve")
Home
Mobile Site | Full Site