Mình nghĩ ra bài này thấy hay hay và viết nó bằng ứng dụng RMI để chạy và thực thi các giao diện tự tạo. cái này là trong phần lập trình mạng.
bước 1: Đầu tiên các bạn bật của sổ cmd lên ra gõ lệnh chuyển tới thư mục chưa các file.java.
bước 2: compiler tất cả các file.java bằng lệnh
javac *.java.
bước 3: chạy các gói *Impl để đăng kí với rmi và tạo ra các gói _stub.class bằng lệnh:
rmic fileImpl (ví dụ: rmic HanoiImpl ...)bước 4: gõ lệnh
start để mở ra của sổ cmd mới, bạn cần thêm 2 của sổ nữa để chạy 1 thằng server và 1 chạy client.
bước 5: đăng kí với RMI bằng cách gõ kệnh
rmiregistry ở cửa sổ cmd thứ nhất.
bước 6: cửa sổ thứ 2 chạy chương trình server bằng lệnh
java CTServerbước 7: cửa sổ thứ 3 chạy chương trình client bằng lệnh
java CTClientbước 8: bạn xem kết quả nhé. :?: :?:
Code đây:Hanoi.java - Code:
-
import java.rmi.*;
public interface Hanoi extends Remote{
public String getHanoi() throws RemoteException;
}
HanoiImpl.java - Code:
-
import java.rmi.*;
public class HanoiImpl implements Hanoi {
public String getHanoi() throws RemoteException{
return Double.toString(Math.random()*150);
}
}
London.java - Code:
-
import java.rmi.*;
public interface London extends Remote{
public String getLondon() throws RemoteException;
}
LondonImpl.java - Code:
-
import java.rmi.*;
public class LondonImpl implements London {
public String getLondon() throws RemoteException{
return Double.toString(Math.random()*150);
}
}
Tokyo.java - Code:
-
import java.rmi.*;
public interface Tokyo extends Remote{
public String getTokyo() throws RemoteException;
}
TokyoImpl.java - Code:
-
import java.rmi.*;
public class TokyoImpl implements London {
public String getTokyo() throws RemoteException{
return Double.toString(Math.random()*150);
}
}
Pari.java - Code:
-
import java.rmi.*;
public interface Pari extends Remote {
public String getPari() throws RemoteException;
}
PariImpl.java - Code:
-
import java.rmi.*;
public class PariImpl implements Pari {
public String getPari() throws RemoteException{
return Double.toString(Math.random()*150);
}
}
NewYork.java - Code:
-
import java.rmi.*;
public interface NewYork extends Remote{
public String getNewYork() throws RemoteException;
}
NewYorkImpl.java - Code:
-
import java.rmi.*;
public class NewYorkImpl implements NewYork {
public String getNewYork() throws RemoteException{
return Double.toString(Math.random()*150);
}
}
Time.java - Code:
-
import java.rmi.*;
import java.util.Date;
import javax.xml.crypto.Data;
public interface Time extends Remote {
public Date getCurrentTime() throws RemoteException;
}
TimeImpl.java - Code:
-
import java.rmi.*;
import java.util.*;
public class TimeImpl implements Time {
public Date getCurrentTime() throws RemoteException{
return new Date();
}
}
RateServer.java - Code:
-
import java.rmi.*;
public interface RateServer extends Remote{
public Hanoi rateHanoi() throws RemoteException;
public London rateLondon() throws RemoteException;
public NewYork rateNewYork() throws RemoteException;
public Pari ratePari() throws RemoteException;
public Tokyo rateTokyo() throws RemoteException;
public Time CreateTime() throws RemoteException;
}
RateServerImpl.java - Code:
-
import java.rmi.*;
import java.rmi.server.*;
public class RateServerImpl implements RateServer {
Hanoi vn = new HanoiImpl();
London el = new LondonImpl();
NewYork us = new NewYorkImpl();
Pari fr = new PariImpl();
Tokyo jp = new TokyoImpl();
Time time = new TimeImpl();
public RateServerImpl(){
try{
UnicastRemoteObject.exportObject(vn);
UnicastRemoteObject.exportObject(el);
UnicastRemoteObject.exportObject(us);
UnicastRemoteObject.exportObject(fr);
UnicastRemoteObject.exportObject(jp);
UnicastRemoteObject.exportObject(time);
}
catch(Exception e){
}
}
public Time CreateTime() throws RemoteException{
return time;
}
public Hanoi rateHanoi() throws RemoteException{
return vn;
}
public London rateLondon() throws RemoteException{
return el;
}
public NewYork rateNewYork() throws RemoteException{
return us;
}
public Pari ratePari() throws RemoteException{
return fr;
}
public Tokyo rateTokyo() throws RemoteException{
return jp;
}
}
CTServer.java - Code:
-
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class CTServer {
public static void main(String[] args){
try{
RateServer sv = new RateServerImpl();
UnicastRemoteObject.exportObject(sv);
Naming.bind("rmi://127.0.0.1/rate", sv);
System.out.print("Server dang doc du lieu...");
}
catch(Exception e){
System.out.print(e);
}
}
}
CTClient.java - Code:
-
import java.awt.*;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.*;
public class CTClient{
public static void main(String[] chuoi) throws Exception{
Frame myWindow = new Frame("Ti gia chung khoan");
TextArea msg = new TextArea("Waiting...");
msg.setBounds(new Rectangle(30, 60, 300, 250));
myWindow.setLayout(null);
myWindow.add(msg,null);
myWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
myWindow.setSize(new Dimension(350, 400));
myWindow.show();
RateServer rs = (RateServer)Naming.lookup("rmi://127.0.0.1/rate");
Hanoi vn = rs.rateHanoi();
London el = rs.rateLondon();
NewYork us = rs.rateNewYork();
Pari fr = rs.ratePari();
Tokyo jp = rs.rateTokyo();
Time time = rs.CreateTime();
while(true){
msg.setText("Current "+time.getCurrentTime()+"\nHa noi: "+vn.getHanoi()+"\nLondon: "+el.getLondon()+"\nNewYork: "+us.getNewYork()+"\nPari: "+fr.getPari()+"\nTokyo: "+jp.getTokyo());
Thread.sleep(1000);
}
}
}
:lol!:
Chúc các bạn vui vẻ :lol: