dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
May 20, 2021

How to Provide Permission to Sockets in 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")
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date