Tuesday, January 15, 2019

Net Bean RMI

 


Write Program in DefaultPackage. (point to note)

 

// Rem.java

import java.rmi.*;
public interface Rem extends Remote{

   
public int addNum(int a,int b)throws RemoteException;

   
public int subNum(int a,int b)throws RemoteException;
}

 

 

// Rem_impt.java

import java.rmi.*;
import java.rmi.server.*;

public class Rem_impt extends UnicastRemoteObject implements Rem{

    Rem_impt()
throws RemoteException{}

   
public int addNum(int a,int b){return a+b;}

   
public int subNum(int a,int b){return a-b;}

}

 

//Server.java

import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;

public class Server{

   
public static void main(String args[]){
       
try{
            Rem_impt locobj =
new Rem_impt();
            Registry rgsty = LocateRegistry.createRegistry(
1888);
            rgsty.rebind(
"Rem", locobj);


        }
catch(RemoteException re){System.out.println(re);}
    }
}
 
 
//Client.java

import java.rmi.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.rmi.registry.*;

public class Client{

   
public static void main(String args[]){

        Scanner sc =
new Scanner(System.in);
        System.
out.println("Enter first number");
       
int a = sc.nextInt();
        System.
out.println("Enter first number");
       
int b = sc.nextInt();
       
try{

            Rem_impt remObj =
new Rem_impt();
            Registry rgsty = LocateRegistry.getRegistry(
1888);
            rgsty.rebind(
"Rem", remObj);
            System.
out.println("Sum = " + remObj.addNum(a,b));
            System.
out.println("Sum = " + remObj.subNum(a,b));
        }
catch(RemoteException re){System.out.println(re);}
    }
}

 

 

Reference:

https://stackoverflow.com/questions/15685686/java-rmi-connectexception-connection-refused-to-host-127-0-1-1



Point 1 :


Oh but it would, observing it on my Archlinux box now. Creating the registry with
         
         LocateRegistry.createRegistry(1099) 
         
in my server class rather than getting it with 
 
         LocateRegistry.getRegistry() 
 
solves the problem for me.

 

Point 2 :

When I got the same error on my machine ("connection is refused"), the reason was that I had defined the following on the server side:

    Naming.rebind(
"rmi://localhost:8080/AddService"
   
,addService);
   
 
Thus the server binds both the IP = 127.0.0.1 and the port 8080.
But on the client side I had used:

    AddServerInterface st = (AddServerInterface)Naming.lookup(
"rmi://localhost"
   
+"/AddService");

Thus I forgot to add the port number after the localhost, so I rewrote the above command and added the port number 8080 as follows:

    AddServerInterface st = (AddServerInterface)Naming.lookup(
"rmi://localhost:8080"
   
+"/AddService");
  
 
 and everything worked fine.

 

 

 

No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...