Showing posts with label Networking. Show all posts
Showing posts with label Networking. Show all posts

Saturday, February 13

RPC Sample code in Java



Hello friends,
In this post we will see how RPC (Remote Procedure Call) mechanism can be implemented in Java.
RMI (Remote Method Invocation)  is the special mechanism to do  RPC in Java. RMI is the java flavor of RPC in other languages, but core concept is still same!

The main Idea is summarized as below:

 In this example, we make a server (RMIServer.java) to receive input text from client application and replies how many characters it received.


Server Side:
  • Create a remote interface. (myInterface.java)
  • Create a separate java file to implement the remote interface. (RMIServer.java)
  • Register the interface in RMI registry (bind with any name eg: myRMIService)
Client Side:
  • In client code, through interface name (in our case myRMIService) create a "fake remote" object reference of server within client. (RMIClient.java)
  • Through this reference access the server methods / services


Let us have a detailed look on code:

1. MyInterface.java

  1. import java.rmi.*;
  2. public interface MyInterface extends Remote
  3. {
  4.  public String countInput(String input)throws RemoteException;   
  5. }

  • It simply says about a remote service to accept a string input and gives string output.
  • Line no 4  insists that any code implementing this interface should have countInput method.

2. RMIServer.java
  1. import java.rmi.*;
  2. import java.rmi.server.*;
  3. public class RMIServer extends UnicastRemoteObject implements MyInterface
  4.     public RMIServer()throws RemoteException
  5.     { 
  6.         System.out.println("Remote Server is running Now.!!"); 
  7.     }    
  8. public static void main(String arg[])
  9.     try{ 
  10.         RMIServer p=new RMIServer();
  11.         Naming.rebind("rmiInterface",p);
  12.     }  
  13. catch(Exception e)
  14. { System.out.println("Exception occurred : "+e.getMessage()); } 
  15. }

  16.     @Override
  17.     public String countInput(String input) throws RemoteException 
  18.     {
  19.     System.out.println("Received your input "+ input+"  at server!!");
  20.         String reply;
  21.         reply="You have typed "+ input.length() +"  letters!!";
  22.         return reply;
  23.     }
  24. }
  • Line 3 says we are going to implement MyInterface interface
  • Line 13 says we register this service with name rmiInterface. Any client can access the services from this class using this name.
  • Line 19-27 implements the methods offered by interface.
3. RMIClient.java
  1. import java.rmi.*;
  2. import java.io.*; 
  3. public class RMIClient
  4. {   
  5.     public static void  main(String args[])
  6.     { 
  7.         try
  8.       { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
  9.       MyInterface p=( MyInterface)Naming.lookup("rmiInterface");
  10.         System.out.println("Type something..."); 
  11.         String input=br.readLine(); 
  12.         System.out.println(p.countInput(input)); 
  13.             }
  14.         catch(Exception e) { 
  15.             System.out.println("Exception occurred : "+e.getMessage());
  16.         }
  17.     } 
  18.  
Line 9:  Looking for the remote service using the name!
Line 12: Accessing the service using the remote object.

RESULT:


Compile the java files.



Run remote object registry  called  rmiregistry. It is used by RMI servers on the same host to bind remote objects to names.


Run the Server class.

Run the client class. You can see it is prompting for input


Type some text
Input is received by remote server
   
                                          


Client receives reply from remote server



Hope this post was useful!!


Feel free to post your suggestions and comments!

Sunday, January 11

INTERNET SECURITY : Networking Lecture Notes

TOPIC: INTERNET SECURITY



What is IPSec?
IPSecurity (IPSec) is a collection of protocols designed to provide security at the network layer.

Transport mode:  The IPSec header and trailer are added to the information  corning from the transport layer. The IP header is added later.

Tunnel mode: It takes an IP packet, including the header, applies IPSec security methods to the entire packet, and then adds a new IP header .


What are the protocols associated with IPSec?

Authentication Header (AH): provides source authentication and data integrity. The protocol uses a hash function and a symmetric key to create a message digest;


Encapsulating Security Payload (ESP) :source authentication ,data integrity and privacy


What are the reserved Addresses for private networks?
Prefix Range Total
10/8 10.0.0.0 to 10.255.255.255 2^24
172.16/12 172.16.0.0 to 172.31.255.255 2^20
192.168/16 192.168.0.0 to 192.168.255.255 2^16

What is Virtual private Network?

Virtual private network (VPN) is a technology that is  used by large organizations that use the global Internet for both intra- and inter organization communication, but require privacy in their internal communications.

VPN technology uses IPSec in the tunnel mode to provide authentication, integrity, and privacy.
All messages for internal communication will travel through Internet after encapsulated inside another packet using IPSec Tunneling.

The public network (Internet) is responsible for carrying the packet from Source side router  to destination side router. Outsiders cannot decipher the contents of the packet or the
source and destination addresses. Deciphering takes place at destination router, which finds the destination address of the packet and delivers it.

Explain Secure Socket layer (SSL)?

Secure Socket Layer (SSL) is designed to provide security and compression to data generated from the application layer.

1. SSL divides the data into blocks of 214 bytes or less.
2. Each fragment of data is compressed by using one of the lossless compression methods
3. To preserve the integrity of data, SSL uses a keyed-hash function to create a MAC.
4. To provide confidentiality, the original data is encrypted using symmetric key
5. A header is added to the encrypted payload. The payload is then passed to a reliable transport layer protocol.

Transport Layer Security (TLS) is the IETF standard version of SSL.

What is the use of Pretty Good Privacy (PGP)?
 PGP is designed to create authenticated and confidential e-mails.

What is Firewall?

A firewall is a device (usually a router) installed between the internal network of an organization and the rest of the Internet. It is designed to filter some packets.

Packet Filter: It can forward or block packets based on the IP addresses , Transport layer protocol or port addresses. It uses a filtering table to decide which packets must be discarded (not forwarded).

Proxy firewall: The server opens the packet at the application level and finds out if the request is legitimate.