Last active
October 17, 2021 14:52
-
-
Save osyduck/32f7578ada46df3f69e59d96d64f2a29 to your computer and use it in GitHub Desktop.
JAVA RMI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rmi; | |
import java.rmi.Naming; | |
import java.util.Scanner; | |
public class Client { | |
public Client() {} | |
public static void main(String[] args) { | |
try { | |
Scanner sc = new Scanner(System.in); | |
Hitung stub = (Hitung) Naming.lookup("rmi://localhost:2021/Hitung"); | |
while (true) { | |
System.out.println("1. Hitung Luas Persegi"); | |
System.out.println("2. Hitung Luas Segitiga"); | |
System.out.println("3. Hitung Luas Lingkaran"); | |
System.out.println("4. Exit"); | |
System.out.print("Pilih : "); | |
switch (sc.nextInt()) { | |
case 1: | |
System.out.print("Masukan Sisi : "); | |
double s = sc.nextDouble(); | |
System.out.println("Hasil : " + stub.Persegi(s)); | |
break; | |
case 2: | |
System.out.print("Masukan Alas : "); | |
double a = sc.nextDouble(); | |
System.out.print("Masukan Tinggi : "); | |
double t = sc.nextDouble(); | |
System.out.println("Hasil : " + stub.Segitiga(a, t)); | |
break; | |
case 3: | |
System.out.print("Masukan Jari Jari : "); | |
double r = sc.nextDouble(); | |
System.out.println("Hasil : " + stub.Lingkaran(r)); | |
break; | |
case 4: | |
System.out.println("Terima Kasih!"); | |
return; | |
default: | |
System.out.println("Inputan Salah"); | |
break; | |
} | |
} | |
}catch (Exception e) { | |
System.out.println("Client Exception: " + e.toString()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rmi; | |
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
public interface Hitung extends Remote { | |
double Persegi(double sisi) throws RemoteException; | |
double Segitiga(double alas, double tinggi) throws RemoteException; | |
double Lingkaran(double jari) throws RemoteException; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rmi; | |
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
import java.rmi.server.UnicastRemoteObject; | |
public class Server implements Hitung { | |
public Server() {} | |
@Override | |
public double Persegi(double sisi) throws RemoteException { | |
return sisi * sisi; | |
} | |
@Override | |
public double Segitiga(double alas, double tinggi) throws RemoteException { | |
return 0.5 * alas * tinggi; | |
} | |
@Override | |
public double Lingkaran(double jari) throws RemoteException { | |
return 3.14 * jari * jari; | |
} | |
public static void main(String[] args) { | |
try { | |
Server object = new Server(); | |
Hitung stub = (Hitung) UnicastRemoteObject.exportObject((Remote) object, 0); | |
Registry registry = LocateRegistry.createRegistry(2021); | |
registry.rebind("Hitung", stub); | |
System.out.println("Server is Ready"); | |
}catch (Exception e) { | |
System.out.println("Server Exception: " + e.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment