.:For Network Programming ( SCN 4223 ) Purpose Only:.

AC990856 Page

--»Assignment 8 --- RMI Compare Two Number

--RmiFace.java

import java.rmi.*;

public interface RmiFace extends Remote {
// public String about() throws RemoteException;
public String compareNum(integer no1, integer no2 ) throws RemoteException;
}
 
--RmiServer.java
 

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

public class RmiServer extends UnicastRemoteObject implements RmiFace {
String result;

public RmiServer() throws RemoteException {
System.out.println("Server busy...");
}

public String compareNum(integer no1, integer no2){
try{
if (no1>no2){
result=("no1");
}
if (no1 result=("no2");
}
if (no1==no2){
result=(""It's a same number");
}
}
catch(Exception excp){}

return result;
}


/** Main method.
*/
public static void main(String[] args) {
try {
RmiServer Server = new RmiServer();
Naming.rebind("xx",Server);
System.out.println("Server is waiting...");
}
catch (RemoteException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

}
 

--RmiClient.java
 

import java.rmi.*;
import java.rmi.registry.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class RmiClient extends javax.swing.JFrame {
double num1,num2;

public RmiClient(){
//interface
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtNom1 = new javax.swing.JTextField();
txtNom2 = new javax.swing.JTextField();
BtnBanding = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();

getContentPane().setLayout(null);

setTitle("Tugasan 8-Perbandingan Dua Nombor : ");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 18));
jLabel1.setText("Perbandingan Dua Nombor");
getContentPane().add(jLabel1);
jLabel1.setBounds(100, 10, 198, 22);

jLabel2.setText("Nombor 1 :");
getContentPane().add(jLabel2);
jLabel2.setBounds(40, 60, 61, 16);

jLabel3.setText("Nombor 2 :");
getContentPane().add(jLabel3);
jLabel3.setBounds(40, 90, 61, 16);

getContentPane().add(txtNom1);
txtNom1.setBounds(120, 60, 80, 20);

getContentPane().add(txtNom2);
txtNom2.setBounds(120, 90, 80, 20);

BtnBanding.setText("Banding");
BtnBanding.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnBandingActionPerformed(evt);
}
});

getContentPane().add(BtnBanding);
BtnBanding.setBounds(240, 85, 80, 26);

jLabel4.setText("Result :");
getContentPane().add(jLabel4);
jLabel4.setBounds(60, 130, 800, 16);
setSize(400,200);
}

private void BtnBandingActionPerformed(java.awt.event.ActionEvent evt) {
initVar();
}


/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public void initVar(){
//DO THIS WHEN USER CLICK BUTTON
num1=Integer.parseInt(txtNom1.getText());
num2=Integer.parseInt(txtNom2.getText());

compare();
}


public void compare() {
//GET RESUL FROM RMI SERVER AND THEN MANIPULATE IT
String compr;
try{
RmiFace rmifce=(RmiFace)Naming.lookup("//127.0.0.1/xx");
compr=rmifce.compareNum(num1,num2);
if (compr.equals("num1")){
jLabel4.setText("Result : Nilai Masukan 1 Lebih Besar : "+num1);
System.out.println("Nilai Masukan "+compr+" Lebih Besar. "+num1+" > "+num2);
}
if (compr.equals("num2")){
jLabel4.setText("Result : Nilai Masukan 2 Lebih Besar : "+num2);
System.out.println("Nilai Masukan "+compr+" Lebih Besar. "+num2+" > "+num1);
}
if (compr.equals("sama")){
jLabel4.setText("Result : Nilai Masukan Sama");
System.out.println("Nilai Masukan Sama"+num1+"="+num2);
}
}

catch(Exception exc){
System.out.println("Error"+exc);
}
}

public static void main(String args[]) {
new RmiClient().show();
}

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txtNom1;
private javax.swing.JTextField txtNom2;
private javax.swing.JButton BtnBanding;
private javax.swing.JLabel jLabel4;

}
 

Copyright @2003 Ac990856
U6C3-9,Kolej Perdana.
Tel: 6012-7615174 ;
e-mail: masalapang@hotmail.com.my