Thực hành Rmi

16
TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA CÔNG NGHỆ THÔNG TIN BỘ MÔN MẠNG VÀ TRUYỀN THÔNG LẬP TRÌNH MẠNG NÂNG CAO RMI Người hướng dẫn : TS. Huỳnh Công Pháp Sinh viên : Phan Hồ Quốc Việt Nhóm : 12 Lớp : 08T2

description

Các bài tập Rmi cơ bản

Transcript of Thực hành Rmi

Page 1: Thực hành Rmi

TRƯỜNG ĐẠI HỌC BÁCH KHOA

KHOA CÔNG NGHỆ THÔNG TIN

BỘ MÔN MẠNG VÀ TRUYỀN THÔNG

LẬP TRÌNH MẠNG NÂNG CAO

RMI

Người hướng dẫn : TS. Huỳnh Công PhápSinh viên : Phan Hồ Quốc ViệtNhóm : 12Lớp : 08T2

Đà Nẵng 2012

Page 2: Thực hành Rmi

BÀI TẬP RMI

Bài 1: Viết ứng dụng RMI, chương trình ở máy Client nhận vào một chuổi s, gọi hàm reverseString(String s) nằm ở Server, hàm này có tác dụng trả vể chuổi đảo ngược của chuổi s.

InverString.java:

import java.rmi.*;

public interface InverString extends Remote {

public String reverseString(String s) throws RemoteException;

}

InverStringImp.java:import java.rmi.*;

public class InverStringImp implements InverString {

public String reverseString(String s) { return new

StringBuilder(s).reverse().toString();}

}StringServer.java:

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

public class StringServer {

public static void main(String[] args) {

try {

InverStringImp c = new InverStringImp();System.out.print("start");UnicastRemoteObject.exportObject(c);

Naming.rebind("rmi://localhost/inverString", c);

Page 3: Thực hành Rmi

System.out.println("Register inver string");

} catch (Exception e) {

System.out.println(e);}

}

}

Bài 2 : Viết ứng dụng RMI, chương trìng ở máy Client nhận vào một số nguyên n, gọi các hàm int sum(int n), long factorial(int n) ở máy server. hàm sum(..) có tác dụng tính tổng 1+2+3...+n, hàm factorial(..) có tác dụng tính n! =1.2.3....n.

import java.rmi.Remote;import java.rmi.RemoteException;import java.rmi.server.RemoteObject;

public interface Sum extends Remote {

public int sum(int n) throws RemoteException;

}

import java.rmi.RemoteException;

public class SumIm implements Sum {

public int sum(int n) throws RemoteException {// TODO Auto-generated method stubint s=0;

for (int i = 0; i < n; i++) {s+=i;

}return s;

}

}client

import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.util.Scanner;

Page 4: Thực hành Rmi

public class Client {

/** * @param args */public static void main(String[] args) {

// TODO Auto-generated method stub try {

Sum obj= (Sum)Naming.lookup("rmi://localhost/testhere");

Scanner nhapvao = new Scanner(System.in); System.out.print("a= "); int a = nhapvao.nextInt(); System.out.println("Ket qua "+obj.sum(a));

} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();

} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();

} catch (NotBoundException e) {// TODO Auto-generated catch blocke.printStackTrace();

}}

}

serverimport java.rmi.Naming;import java.rmi.server.UnicastRemoteObject;

public class Server {public static void main(String[] args) { try { SumIm c = new SumIm(); UnicastRemoteObject.exportObject(c); Naming.bind("rmi://localhost/testhere", c); System.out.println("Register...!!!"); } catch (Exception e) { System.out.println("Loi" + e); } }

}

Page 5: Thực hành Rmi

Bài 3: Viết ứng dụng RMI, chuơng trình ở máy Client nhận vào 2 chuổi username,password, gọi hàm boolean CheckAccount(String username, String password) nằm ở máy Server, hàm này có tác dụng truy cập đến CSDL để kiểm tra xem account trên có tồn tại không. nếu có trả về true ngược lại trả về false.

import java.rmi.Remote;import java.rmi.RemoteException;

public interface CSDLLogin extends Remote{

public boolean Check(String a ,String b) throws RemoteException;

}import java.rmi.RemoteException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.*;

public class CSDLLoginIm implements CSDLLogin {

Connection con;public CSDLLoginIm(){ try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");String Url="jdbc:odbc:Data";try {

con = DriverManager.getConnection(Url);

} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();

}

} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();

}

}

Page 6: Thực hành Rmi

public boolean Check(String a, String b) throws RemoteException {

// TODO Auto-generated method stub ResultSet kq=null;

Statement stCmd;try {

stCmd = con.createStatement(); String sqlQuery = "select * from

login"; kq = stCmd.executeQuery(sqlQuery); while(kq.next()) { String ms=kq.getString("Name"); String mk=kq.getString("pass"); if(ms.equals(a)&&mk.equals(b)) { return true; }

} }catch (SQLException e) {

// TODO Auto-generated catch blocke.printStackTrace();

}

return false;}

}

clientimport java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.util.Scanner;

public class Client {

/** * @param args */public static void main(String[] args) {

// TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("Nhap user "); String a = input.nextLine(); System.out.println("Nhap pass ");

Page 7: Thực hành Rmi

String b = input.nextLine(); try {

CSDLLogin check = (CSDLLogin) Naming.lookup("rmi://localhost/enter");

if(check.Check(a, b)){

System.out.println("dang nhap tc ");

}else{

System.out.println("That bai ");}

} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();

} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();

} catch (NotBoundException e) {// TODO Auto-generated catch blocke.printStackTrace();

}

}

}Server

import java.rmi.Naming;import java.rmi.server.UnicastRemoteObject;

public class Server { public static void main(String a[]) {

try {

CSDLLoginIm c = new CSDLLoginIm(); UnicastRemoteObject.exportObject(c); Naming.bind("rmi://localhost/enter", c); System.out.println("Register...!!!"); } catch (Exception e) { System.out.println("Loi" + e); }

Page 8: Thực hành Rmi

}}

Bài 4:Viết ứng dụng RMI tra cứu thông tin về địa danh như sau :+ Xây dựng CSDL có Table LocationInfor như sau :+ Xây dựng chương trình Server gồm các hàm sau :

* String getInfor(String Location)Hàm này truy cập và lấy thông trong CSDL. Nếu tìm thấy trả về chuổi

thông tin tìmđược, ngược lại trả về chuổi “ Information is not found “ .

* boolean saveInfor(String Location, String Address, String phone, String Note)Hàm này dùng để Insert thông tin vào CSDL.

import java.rmi.Remote;import java.rmi.RemoteException;

public interface CSDLinfor extends Remote {

public String getInfor(String a) throws RemoteException; public boolean SaveInfor(String a, String b,String c,String d) throws RemoteException;}import java.rmi.RemoteException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;

public class CSDLinforIm implements CSDLinfor {

Connection con;public CSDLinforIm() {

// TODO Auto-generated constructor stubtry {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");con =

DriverManager.getConnection("jdbc:odbc:DataLocal");

} catch (ClassNotFoundException e) {

Page 9: Thực hành Rmi

// TODO Auto-generated catch blocke.printStackTrace();

} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();

}

}

@Overridepublic String getInfor(String a) throws

RemoteException {// TODO Auto-generated method stubResultSet kq=null;

Statement stCmd;try {

stCmd = con.createStatement(); String sqlQuery = "select * from

LocationInfor where Location = '"+a+"' "; kq = stCmd.executeQuery(sqlQuery); while(kq.next()) { return (kq.getString("Location")

+":"+kq.getString("Address")+":"+kq.getString("Phone")+":"+kq.getString("Note")+" ");

} }catch (SQLException e) {

// TODO Auto-generated catch blocke.printStackTrace();

}return "Khong tim thay";

}

@Overridepublic boolean SaveInfor(String a, String b, String c,

String d)throws RemoteException {

// TODO Auto-generated method stubResultSet kq=null;

Statement stCmd;

try {

stCmd = con.createStatement();String insert="insert into

LocationInfor values('"+a+"','"+b+"','"+c+"','"+d+"')";stCmd.execute(insert);return true;

Page 10: Thực hành Rmi

} catch (SQLException e) {// TODO Auto-generated catch block

e.printStackTrace();return false;

}

}

}

Client

import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.LayoutManager;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.util.Scanner;

import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;

public class Client extends JFrame implements ActionListener {

/** * @param args * @throws NotBoundException * @throws RemoteException * @throws MalformedURLException */private CSDLinfor a;private JButton btfind = new JButton("find");private JButton btSave = new JButton("Save");private JTextField tx =new JTextField(30);private JTextArea ta = new JTextArea(10,10);public Client() throws MalformedURLException,

RemoteException, NotBoundException{

Page 11: Thực hành Rmi

a =(CSDLinfor) Naming.lookup("rmi://localhost/infor");

setSize(500, 200); JPanel p0 = new JPanel(); p0.setLayout(new FlowLayout()); p0.add(tx); p0.add(btfind); p0.add(btSave); add(ta,BorderLayout.SOUTH); add(p0,BorderLayout.NORTH); // btfind.addActionListener(this); btSave.addActionListener(this);

this.pack();

}public static void main(String[] args) throws

MalformedURLException, RemoteException, NotBoundException {// TODO Auto-generated method stub

Client a = new Client(); a.show();}@Overridepublic void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stubif(e.getSource()==btfind){

String infor =tx.getText();try {

ta.setText(a.getInfor(infor));

} catch (RemoteException e1) {// TODO Auto-generated catch blocke1.printStackTrace();

}

}if(e.getSource()==btSave){

String infor =tx.getText();try {

a.SaveInfor(infor, infor, infor, infor);

Page 12: Thực hành Rmi

} catch (RemoteException e1) {// TODO Auto-generated catch blocke1.printStackTrace();

}

}}

}

serverimport java.net.MalformedURLException;import java.rmi.AlreadyBoundException;import java.rmi.Naming;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;

public class Server {

public static void main(String a0[]) throws RemoteException, MalformedURLException, AlreadyBoundException

{CSDLinforIm a =new CSDLinforIm();UnicastRemoteObject.exportObject(a);Naming.bind("rmi://localhost/infor", a);

}}

Kết Quả

Page 13: Thực hành Rmi