Socket Programming connecting processes SACHIN KHARADE Elements of

  • Slides: 65
Download presentation
Socket Programming connecting processes SACHIN KHARADE

Socket Programming connecting processes SACHIN KHARADE

Elements of C-S Computing Client Re qu es t a client, a server, and

Elements of C-S Computing Client Re qu es t a client, a server, and network Re su Client machine Network Server lt Server machine SACHIN KHARADE

Networking Basics • Computers running on the Internet communicate with each other using either

Networking Basics • Computers running on the Internet communicate with each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP) SACHIN KHARADE

DNS - Domain name system • The Domain Name system (DNS) associates various sorts

DNS - Domain name system • The Domain Name system (DNS) associates various sorts of information with so-called domain names. • Most importantly, it serves as the "phone book" for the Internet by translating human-readable computer hostnames, e. g. www. example. com, into the IP addresses, e. g. 208. 77. 188. 166, that networking equipment needs to deliver information. • It also stores other information such as the list of mail exchange servers that accept email for a given domain. SACHIN KHARADE

Understanding Ports • The TCP and UDP protocols use ports to map incoming data

Understanding Ports • The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. server P o r t app app port TCP or UDP SACHIN KHARADE Data TCP Client Packet port# data

Introduction to Sockets SACHIN KHARADE

Introduction to Sockets SACHIN KHARADE

Socket sockets (end points) logical connection A socket is an endpoint for communication between

Socket sockets (end points) logical connection A socket is an endpoint for communication between two machines. The combination of an IP address and a port number. SACHIN KHARADE

 • Why Sockets? – Used for Interprocess communication. • The Client-Server model –

• Why Sockets? – Used for Interprocess communication. • The Client-Server model – Most interprocess communication uses client-server model – Client & Server are two processes that wants to communicate with each other – The Client process connects to the Server process, to make a request for information/services own by the Server. – Once the connection is established between Client process and Server process, they can start sending / receiving information. • What are Sockets? – End-point of interprocess communication. Socket – An interface through which processes can send / receive information SACHIN KHARADE

 • What exactly creates a Socket? – <IP address, Port #> tuple •

• What exactly creates a Socket? – <IP address, Port #> tuple • What makes a connection? – {Source<IP address, Port #> , Destination <IP address, Port #>} i. e. source socket – destination socket pair uniquely identifies a connection. • Example 1343 Client 192. 168. 0. 2 Server 80 1343 Client 192. 168. 0. 3 192. 168. 0. 1 5488 Client 192. 168. 0. 2 SACHIN KHARADE

 • Socket Types – STREAM – uses TCP which is reliable, stream oriented

• Socket Types – STREAM – uses TCP which is reliable, stream oriented protocol, – DATAGRAM – uses UDP which is unreliable, message oriented protocol – RAW – provides RAW data transfer directly over IP protocol (no transport layer) • Sockets can use – “unicast” ( for a particular IP address destination) – “multicast” ( a set of destinations – 224. x. x. x) – “broadcast” (direct and limited) – “Loopback” address i. e. 127. x. x. x SACHIN KHARADE

Transmission Control Protocol • A connection-based protocol that provides a reliable flow of data

Transmission Control Protocol • A connection-based protocol that provides a reliable flow of data between two computers. • Provides a point-to-point channel for applications that require reliable communications. – The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of applications that require a reliable communication channel • Guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. Otherwise, an error is reported. SACHIN KHARADE

User Datagram Protocol • A protocol that sends independent packets of data, called datagrams,

User Datagram Protocol • A protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connectionbased like TCP and is not reliable: – Sender does not wait for acknowledgements – Arrival order is not guaranteed – Arrival is not guaranteed • Used when speed is essential, even in cost of reliability – e. g. streaming media, games, Internet telephony, etc. SACHIN KHARADE

Proxy Server • Proxy servers are related to firewalls. If a firewall prevents hosts

Proxy Server • Proxy servers are related to firewalls. If a firewall prevents hosts on a network from making direct connections to the outside world, a proxy server can act as a go-between. • Thus, a machine that is prevented from connecting to the external network by a firewall would make a request for a web page from the local proxy server instead of requesting the web page directly from the remote web server. SACHIN KHARADE

 • The proxy server would then request the page from the web server

• The proxy server would then request the page from the web server and forward the response back to the original requester. • One of the security advantages of using a proxy server is that external hosts only find out about the proxy server. They do not learn the names and IP addresses of the internal machines, making it more difficult to hack into internal systems. SACHIN KHARADE

Internet Addressing • Handling internet addresses (domain names, and IP addresses) is made easy

Internet Addressing • Handling internet addresses (domain names, and IP addresses) is made easy with Java. Internet addresses are represented in Java by the Inet. Address class. • Inet. Address provides simple methods to convert between domain names, and numbered addresses. SACHIN KHARADE

Inet. Address • The Inet. Address class is used to encapsulate both the numerical

Inet. Address • The Inet. Address class is used to encapsulate both the numerical IP address and the domain name for that address. • We interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. • The Inet. Address class hides the number inside. SACHIN KHARADE

Inet. Address class • static methods you can use to create new Inet. Address

Inet. Address class • static methods you can use to create new Inet. Address objects. – get. By. Name(String host) – get. All. By. Name(String host) – get. Local. Host() Inet. Address x = Inet. Address. get. By. Name( “msbte. com”); v Throws Unknown. Host. Exception SACHIN KHARADE

Factory Methods • static Inet. Address get. Local. Host( ) throws Unknown. Host. Exception

Factory Methods • static Inet. Address get. Local. Host( ) throws Unknown. Host. Exception – returns the IP address of the localhost machine. • static Inet. Address get. By. Name(String host. Name)throws Unknown. Host. Exception – returns an Inet. Address instance representing the hostname. • static Inet. Address[ ] get. All. By. Name(String host. Name) throws Unknown. Host. Exception – returns an array of Inet. Address instances representing the hostname. SACHIN KHARADE

Factory Methods Example class Inet. Address. Test { public static void main(String args[]) throws

Factory Methods Example class Inet. Address. Test { public static void main(String args[]) throws Unknown. Host. Exception { Inet. Address = Inet. Address. get. Local. Host(); System. out. println(Address); Address = Inet. Address. get. By. Name("www. javatpont. com"); System. out. println(Address); Inet. Address SW[] = Inet. Address. get. All. By. Name("www. yahoo. com"); for (int i=0; i<SW. length; i++) System. out. println(SW[i]); } } SACHIN KHARADE

Instance Methods • • • boolean equals(Object obj) – Compares two IP addresses, returns

Instance Methods • • • boolean equals(Object obj) – Compares two IP addresses, returns true if there is a match. byte[ ] get. Address( ) – Returns the IP address in byte format. String get. Host. Address( ) – Returns the IP address in dotted decimal format. String get. Host. Name( ) – Returns the hostname of the Inet. Address. boolean is. Multicast. Address( ) – Returns true if the Inet. Address is a multicast address (class D address). String to. String() – Converts this IP address to a String. SACHIN KHARADE

Instance Methods class Inet. Address. Test 1 { public static void main(String args[])throws Unknown.

Instance Methods class Inet. Address. Test 1 { public static void main(String args[])throws Unknown. Host. Exception { Inet. Address = Inet. Address. get. By. Name("www. google. com"); System. out. println(Address. get. Host. Address()); System. out. println(Address. get. Host. Name()); if(Address. is. Multicast. Address()) System. out. println("It is multicast address"); } } SACHIN KHARADE

TCP/IP Sockets • Server sockets – Wait for requests to come in over the

TCP/IP Sockets • Server sockets – Wait for requests to come in over the network – Implemented by java. net. Server. Socket class • Client sockets – Used to send and receive data – Can be thought of as a pair of input and output streams – Implemented by java. net. Socket class SACHIN KHARADE

Server vr. Client Socket Server socket: waiting for connection requests Client socket: transmission of

Server vr. Client Socket Server socket: waiting for connection requests Client socket: transmission of data server socket connection request client socket SACHIN KHARADE

Server socket The constructors used to server socket are given below. All of them

Server socket The constructors used to server socket are given below. All of them throw IO Exception v Server. Socket (int port) throws Bind. Exception, IOException v creates a server socket bounded to the specified port with a queue length 50. v Server. Socket (int port, int max. Queue) throws Bind. Exception, IOException v creates a server socket bounded to the specified port with a queue length of max. Queue. v Server. Socket (int port, int max. Q, Inet. Address ip) throws IOException v creates a server socket bounded to the specified port with a queue length of max. Queue. On a multihomed host, ip specifies the IP Address to which this socket SACHINbinds. KHARADE

Client socket The constructors used to server socket are given below. All of them

Client socket The constructors used to server socket are given below. All of them throw IO Exception v public Socket(String host, int port) throws Unknown. Host. Exception, IOException. v Creates a socket connecting to the local host to the named host and port v public Socket(Inet. Address host, int port) throws IOException v Creates a socket using a preexisting Inet. Address and port SACHIN KHARADE

Server socket • Methods Some method defined in the Server Socket are: Methods Description

Server socket • Methods Some method defined in the Server Socket are: Methods Description public Socket accept() throws IOException Waits for a connection request and returns a Socket public void set. So. Timeout(int timeout) Sets the time-out value for how long the server socket waits for a client during the accept(). ) public int get. Local. Port() Returns the port number on which this socket is listening SACHIN KHARADE

Methods used by both Server and Client Sockets Methods Description public int get. Port()

Methods used by both Server and Client Sockets Methods Description public int get. Port() Returns the remote port to which this socket is bound. public int get. Local. Port() Returns the port the socket is bound to on the local machine. public Inet. Address get. Inet. Address() Returns IP address to which socket are connected. public Socket. Address get. Local. Address() Returns local address to which socket are connected. public Socket. Address get. Remote. Socket. Address() Returns the address of the remote socket. public Input. Stream get. Input. Stream() throws IOException Returns an Input. Stream for receiving data. public Output. Stream get. Output. Stream() throws IOException Returns an Output. Stream to send data. public void close() throws IOException Closes the socket connection. SACHIN KHARADE

Programming TCP Client-Server in Java • • • All the classes related to sockets

Programming TCP Client-Server in Java • • • All the classes related to sockets are in the java. net package, so make sure to import that package when you program sockets. All the input/output stream classes are in the java. io package, include this also How to open a socket? – If you are programming a client, then you would create an object of Socket class – Machine name is the machine you are trying to open a connection to, – Port. Number is the port (a number) on which the server you are trying to connect to is running. select one that is greater than 1, 023! Why? ? Socket My. Client; try { My. Client = new Socket("Machine name", Port. Number); } catch (IOException e) { System. out. println(e); } SACHIN KHARADE

Programming TCP Client-Server in Java • If you are programming a server, then this

Programming TCP Client-Server in Java • If you are programming a server, then this is how you open a socket: Server. Socket My. Service; try { My. Serverice = new Server. Socket(Port. Number); } catch (IOException e) { System. out. println(e); } • When implementing a server you also need to create a socket object from the Server. Socket in order to listen for and accept connections from clients. Socket client. Socket = null; try { client. Socket = My. Service. accept(); } catch (IOException e) { System. out. println(e); } SACHIN KHARADE

Programming TCP Client-Server in Java • How to create an input stream? – On

Programming TCP Client-Server in Java • How to create an input stream? – On the client side, you can use the Data. Input. Stream class to create an input stream to receive response from the server: Data. Input. Stream input; try { input = new Data. Input. Stream(My. Client. get. Input. Stream()); } catch (IOException e) { System. out. println(e); } – The class Data. Input. Stream allows you to read lines of text and Java primitive data types in a portable way. It has methods such as read, read. Char, read. Int, read. Double, and read. Line, . – On the server side, you can use Data. Input. Stream to receive input from the client: Data. Input. Stream input; try { input = new Data. Input. Stream(client. Socket. get. Input. Stream()); } catch (IOException e) { System. out. println(e); } SACHIN KHARADE

Programming TCP Client-Server in Java • How to create an output stream? – On

Programming TCP Client-Server in Java • How to create an output stream? – On the client side, you can create an output stream to send information to the server socket using the class Print. Stream or Data. Output. Stream of java. io: Print. Stream output; try { output = new Print. Stream(My. Client. get. Output. Stream()); } catch (IOException e) { System. out. println(e); } – The class Print. Stream has methods for displaying textual representation of Java primitive data types. Its write and println methods are important. Also, you may want to use the Data. Output. Stream: Data. Output. Stream output; try { output = new Data. Output. Stream(My. Client. get. Output. Stream()); } catch (IOException e) { System. out. println(e); } – Many of its methods write a single Java primitive type to the output stream. SACHIN KHARADE The method write. Bytes is a useful one.

Programming TCP Client-Server in Java • On the server side – you can use

Programming TCP Client-Server in Java • On the server side – you can use the class Print. Stream to send information to the client. Print. Stream output; try { output = new Print. Stream(client. Socket. get. Output. Stream()); } catch (IOException e) { System. out. println(e); } • Note: You can use the class Data. Output. Stream as mentioned previously. SACHIN KHARADE

Programming TCP Client-Server in Java • How to close sockets? – You should always

Programming TCP Client-Server in Java • How to close sockets? – You should always close the output and input stream before you close the socket. – On the client side: try { output. close(); input. close(); My. Client. close(); } catch (IOException e) { System. out. println(e); } – On the server side: try { output. close(); input. close(); client. Socket. close(); My. Service. close(); } catch (IOException e) { System. out. println(e); } SACHIN KHARADE

A generic UDP application • • algorithm for UDP client – Find the IP

A generic UDP application • • algorithm for UDP client – Find the IP address and port number of server – Create a UDP socket – Send/ receive data with server using the socket – Close the connection algorithm for UDP server – Find the IP address and port number of server – Create a UDP server socket – Bind the server socket to server IP and Port number (this is the port to which clients will send) – Send/ receive data with client using the client socket – Close the connection with client SACHIN KHARADE

Programming UDP Client-Server in Java • How to send/receive on Datagram sockets? – On

Programming UDP Client-Server in Java • How to send/receive on Datagram sockets? – On the client side, you can use the Datagram. Packet class – To send data byte[] buf = new byte[256]; Inet. Address address = Inet. Address. get. By. Name(args[0]); Datagram. Packet packet = new Datagram. Packet(buf, buf. length, address, 4445); socket. send(packet); – To receive data packet = new Datagram. Packet(buf, buf. length); socket. receive(packet); String received = new String(packet. get. Data()); System. out. println(“Received from server: " + received); SACHIN KHARADE

Programming UDP Client-Server in Java • How to send/receive on Datagram sockets? – On

Programming UDP Client-Server in Java • How to send/receive on Datagram sockets? – On the Server side, you can use the Datagram. Packet class – To receive data byte[] buf = new byte[256]; Datagram. Packet packet = new Datagram. Packet(buf, buf. length); socket. receive(packet); • To send data Inet. Address address = packet. get. Address(); int port = packet. get. Port(); packet = new Datagram. Packet(buf, buf. length, address, port); socket. send(packet); • How to close a Datagram socket? socket. close(); SACHIN KHARADE

File: My. Server. java import java. io. *; import java. net. *; public class

File: My. Server. java import java. io. *; import java. net. *; public class My. Server { public static void main(String[] args) { try { Server. Socket ss=new Server. Socket(6666); Socket s=ss. accept(); //establishes connection Data. Input. Stream dis=new Data. Input. Stream(s. get. Input. Stream()); String str=(String)dis. read. UTF(); System. out. println("message= "+str); ss. close(); } catch(Exception e) { System. out. println(e); } } } SACHIN KHARADE

My. Client. java import java. io. *; import java. net. *; public class My.

My. Client. java import java. io. *; import java. net. *; public class My. Client { public static void main(String[] args) { try { Socket s=new Socket("localhost", 6666); Data. Output. Stream dout=new Data. Output. Stream(s. get. Output. Stream()); dout. write. UTF("Hello Server"); dout. flush(); dout. close(); s. close(); } catch(Exception e) { System. out. println(e); } } SACHIN KHARADE }

Example of Java Socket Programming (Read-Write both side) SACHIN KHARADE

Example of Java Socket Programming (Read-Write both side) SACHIN KHARADE

My. Server. java import java. net. *; import java. io. *; class My. Server{

My. Server. java import java. net. *; import java. io. *; class My. Server{ public static void main(String args[])throws Exception{ Server. Socket ss=new Server. Socket(3333); Socket s=ss. accept(); Data. Input. Stream din=new Data. Input. Stream(s. get. Input. Stream()); Data. Output. Stream dout=new Data. Output. Stream(s. get. Output. Stream()); Buffered. Reader br=new Buffered. Reader(new Input. Stream. Reader(System. in)); String str="", str 2=""; while(!str. equals("stop")){ str=din. read. UTF(); System. out. println("client says: "+str); str 2=br. read. Line(); dout. write. UTF(str 2); dout. flush(); } din. close(); ss. close(); }} SACHIN KHARADE

My. Client. java import java. net. *; import java. io. *; class My. Client{

My. Client. java import java. net. *; import java. io. *; class My. Client{ public static void main(String args[])throws Exception{ Socket s=new Socket("localhost", 3333); Data. Input. Stream din=new Data. Input. Stream(s. get. Input. Stream()); Data. Output. Stream dout=new Data. Output. Stream(s. get. Output. Stream()); Buffered. Reader br=new Buffered. Reader(new Input. Stream. Reader(System. in)); String str="", str 2=""; while(!str. equals("stop")){ str=br. read. Line(); dout. write. UTF(str); dout. flush(); str 2=din. read. UTF(); System. out. println("Server says: "+str 2); } dout. close(); s. close(); }} SACHIN KHARADE

URL Class Java URL Class present in java. net package, deals with URL (Uniform

URL Class Java URL Class present in java. net package, deals with URL (Uniform Resource Locator) which uniquely identify or locate resources on internet. SACHIN KHARADE

Constructors of URL class • URL (String urlspecifier) – Creates a URL object from

Constructors of URL class • URL (String urlspecifier) – Creates a URL object from the String representation. • URL (String protocol, String host, int port, String file) – Creates a URL object from the specified protocol, host, port number, and file. • URL (String protocol, String host, String file) – Creates a URL from the specified protocol name, host name, and file name. • URL (URL url. Obj, String urlspecifier) – Creates a URL by parsing the given spec within a specified context. SACHIN KHARADE

Important Methods of URL class • get. Protocol() – Returns protocol of URL •

Important Methods of URL class • get. Protocol() – Returns protocol of URL • get. Host() – Returns hostname(domain name) of URL • get. Port() – Returns port number of URL • get. File() – Returns filename of URL • public URLConnection open. Connection() throws IOException – Creates (if not already in existance) a URLConnection object that contains a connection to the remote object referred to by the URL. • public final Object get. Content() throws IOException – Gets the contents from this opened connection. • public final Input. Stream open. Stream() throws IOException – Opens an input stream. SACHIN KHARADE

import java. net. *; class Test { public static void main(String[] arg) throws Mal.

import java. net. *; class Test { public static void main(String[] arg) throws Mal. Formed. URLException { URL hp = New URL("http: //www. google. com"); System. out. println(hp. get. Protocol[]); System. out. println(hp. get. File[]); } } SACHIN KHARADE

URL Connection Class • The abstract class URLConnection is the superclass of all classes

URL Connection Class • The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL. SACHIN KHARADE

Constructor of URLConnection class • protected URLConnection(URL url) – Constructs a URL connection to

Constructor of URLConnection class • protected URLConnection(URL url) – Constructs a URL connection to the specified URL. A connection to the object referenced by the URL is not created. SACHIN KHARADE

Methods of URLConnection class • public abstract void connect()throws IOException – Opens a communications

Methods of URLConnection class • public abstract void connect()throws IOException – Opens a communications link to the resource referenced by this URL. • public URL get. URL() – Returns the value of this URLConnection's URL field. • public int get. Content. Length() – Returns the value of the content-length header field. • public String get. Content. Type() – Returns the value of the content-type header field. • public long get. Date() – Returns the value of the date header field. • public Object get. Content()throws IOException – Retrieves the contents of this URL connection. • public Input. Stream get. Input. Stream()throws IOException – Returns an input stream that reads from this open connection. • public Output. Stream get. Output. Stream()throws IOException – Returns an output stream that writes to this connection. SACHIN KHARADE

Introduction to java. security Package • The java. security package contains the classes and

Introduction to java. security Package • The java. security package contains the classes and interfaces that implement the Java security architecture. • These classes can be divided into two broad categories. Ø First, there are classes that implement access control and prevent untrusted code from performing sensitive operations. Ø Second, there authentication classes that implement message digests and digital signatures and can authenticate Java classes and other objects. SACHIN KHARADE

SACHIN KHARADE The access control classes of the java. security package

SACHIN KHARADE The access control classes of the java. security package

The authentication classes of the java. security package SACHIN KHARADE

The authentication classes of the java. security package SACHIN KHARADE

SACHIN KHARADE

SACHIN KHARADE

 • The central access control class is Access. Controller; it uses the currently

• The central access control class is Access. Controller; it uses the currently installed Policy object to decide whether a given class has Permission to access a given system resource. • The key classes for authentication are Message. Digest and Signature; they compute and verify cryptographic message digests and digital signatures. These classes use public-key cryptography techniques and rely on the Public. Key and Private. Key classes. SACHIN KHARADE

 • Digital Signatures. Digital signature algorithms, such as DSA (Digital Signature Algorithm). The

• Digital Signatures. Digital signature algorithms, such as DSA (Digital Signature Algorithm). The functionality includes generating public/private key pairs as well as signing and verifying arbitrary digital data. • Message Digests. Cryptographically secure message digests, such as MD 5 and SHA-1. These algorithms, also called one-way hash algorithms, are useful for producing "digital fingerprints" of data, which are frequently used in digital signatures and other applications that need unique and unforgeable identifiers for digital data. SACHIN KHARADE

 • Key Management - A set of abstractions for managing principals (entities such

• Key Management - A set of abstractions for managing principals (entities such as individual users or groups), their keys, and their certificates. It allows applications to design their own key management systems, and to interoperate with other systems at a high level. Note that support for specific certificate formats is not available but will be part of a future JDK release. • The Key. Pair Class - The Key. Pair class is a simple holder for a key pair (a public key and a private key). It has two public methods, one for returning the private key, and the other for returning the public key. • The Key. Pair. Generator Class - The Key. Pair. Generator class is an engine class used to generate pairs of public and private keys. SACHIN KHARADE

Java Permissions • A permission represents access to a system resource. In order for

Java Permissions • A permission represents access to a system resource. In order for a resource access to be allowed for an applet, the corresponding permission must be explicitly granted to the code attempting the access. • A permission typically has a name and, in some cases, a comma-separated list of one or more actions. For example, perm = new java. io. File. Permission("/tmp/abc", "read"); SACHIN KHARADE

Java Permissions (cont’d…) • All. Permission – The java. security. All. Permission is a

Java Permissions (cont’d…) • All. Permission – The java. security. All. Permission is a permission that implies all other permissions. – This permission should be used only during testing, or in extremely rare cases where an application or applet is completely trusted. SACHIN KHARADE

Java Permissions (cont’d…) • Security. Permission – A java. security. Security. Permission is for

Java Permissions (cont’d…) • Security. Permission – A java. security. Security. Permission is for security permissions. A Security. Permission contains a name (also referred to as a "target name") but no actions list – The target name is the name of a security configuration parameter (see below). Currently the. Security. Permission object is used to guard access to the Policy, Security, Provider, Signer, and Identityobjects. SACHIN KHARADE

Java Permissions (cont’d…) • Unresolved. Permission – The java. security. Unresolved. Permission class is

Java Permissions (cont’d…) • Unresolved. Permission – The java. security. Unresolved. Permission class is used to hold Permissions that were "unresolved" when the. Policy was initialized. An unresolved permission is one whose actual Permission class does not yet exist at the time the Policy is initialized. SACHIN KHARADE

Java Permissions (cont’d…) • AWTPermission – A java. awt. AWTPermission is for AWT permissions.

Java Permissions (cont’d…) • AWTPermission – A java. awt. AWTPermission is for AWT permissions. • File. Permission – A java. io. File. Permission represents access to a file or directory. A File. Permission consists of a pathname and a set of actions valid for that pathname. SACHIN KHARADE

Java Permissions (cont’d…) • Serializable. Permission – A java. io. Serializable. Permission is for

Java Permissions (cont’d…) • Serializable. Permission – A java. io. Serializable. Permission is for serializable permissions. A Serializable. Permission contains a name (also referred to as a "target name") but no actions list; you either have the named permission or you don't. – The target name is the name of the Serializable permission. SACHIN KHARADE

Java Permissions (cont’d…) • Net. Permission – A java. net. Net. Permission is for

Java Permissions (cont’d…) • Net. Permission – A java. net. Net. Permission is for various network permissions. A Net. Permission contains a name but no actions list. • Socket. Permission – A java. net. Socket. Permission represents access to a network via sockets. A Socket. Permission consists of a host specification and a set of "actions" specifying ways to connect to that host. – The possible ways to connect to the host are • accept , connect, listen, resolve SACHIN KHARADE

Java Policy • A Policy object is responsible for determining whether code executing in

Java Policy • A Policy object is responsible for determining whether code executing in the Java runtime environment has permission to perform a security-sensitive operation. • Two packages are used – Java. lang. Object – Java. security. Policy public abstract class Policy extends Object This is an abstract class for representing the system security policy for a Java application SACHIN KHARADE environment.

Java Policy (cont’d…) • There is only one Policy object installed in the runtime

Java Policy (cont’d…) • There is only one Policy object installed in the runtime at any given time. • A Policy object can be installed by calling the set. Policy method. The installed Policy object can be obtained by calling the get. Policy method. SACHIN KHARADE

Java Policy (cont’d…) • public static Policy get. Policy() • Returns the installed Policy

Java Policy (cont’d…) • public static Policy get. Policy() • Returns the installed Policy object. This value should not be cached, as it may be changed by a call to set. Policy. This method first calls Security. Manager. check. Permission with a Security. Permission ("get. Policy") permission to ensure it's ok to get the Policy object. • Throws Security. Exception - if a security manager exists and its check. Permission method doesn't allow getting the Policy object. • public static void set. Policy(Policy policy) • Sets the system-wide Policy object. This method first calls Security. Manager. check. Permission with a Security. Permission ("set. Policy") permission to ensure it's ok to set the Policy. • Throws Security. Exception - if a security manager exists and its check. Permission method doesn't allow setting the Policy. SACHIN KHARADE