RPC/RMI实验

在这里插入图片描述

源码目录结构(服务器端和客户端放在同一项目的不同包中):
在这里插入图片描述
若要分开,服务器端可以直接把RMIclient包删掉
客户端可以这样:
在这里插入图片描述
源码:
1.服务器端:
BOOK.java:

package RMIserve;

import java.io.Serializable;

public class BOOK implements Serializable {
    private static final long serialVersionUID = 1L;

	private static int SUM;
	private int ID;
	private String name;
	
	public BOOK(String name) {
		this.name = name;
		ID = SUM++;
	}
	
	public int getID() {
		return ID;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "ID:" + ID + ", name:" + name ;
	}


}


IBOOK.java

package RMIserve;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.LinkedList;

public interface IBOOK extends Remote{

	public boolean add(BOOK b) throws RemoteException;
	public BOOK queryByID(int bookID) throws RemoteException;
	public LinkedList<BOOK> queryByName(String name) throws RemoteException;
	public boolean delete(int bookID) throws RemoteException;
	
}

IBOOKIMPL.java

package RMIserve;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;


//接口的实现
public class IBOOKIMPL extends UnicastRemoteObject implements IBOOK {
    private static final long serialVersionUID = 1L;

	private static LinkedList<BOOK> BOOKDB = new LinkedList<BOOK>();

	protected IBOOKIMPL() throws RemoteException {
		// TODO 自动生成的构造函数存根
	}


	public boolean add(BOOK b) throws RemoteException {
		
		return BOOKDB.add(b);
	}

	@Override
	public BOOK queryByID(int bookID) throws RemoteException {
		// TODO 自动生成的方法存根
		BOOK b = null;
		for(BOOK book:BOOKDB) {
			if(book.getID()==bookID) {
				b = book;
			}
		}
		return b;
	}

	public LinkedList<BOOK> queryByName(String name)  throws RemoteException {
		// TODO 自动生成的方法存根
		LinkedList<BOOK> B = new LinkedList<BOOK>();
		for(BOOK book:BOOKDB) {
			if(book.getName().equals(name)) {
				B.add(book);
			}
		}
		return B;
	}

	@Override
	public boolean delete(int bookID)  throws RemoteException {
		// TODO 自动生成的方法存根
		for(BOOK book:BOOKDB) {
			if(book.getID()==bookID) {
				return BOOKDB.remove(book);
			}
		}
		return false;
	}


}

Server.java:

package RMIserve;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class Server {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		try {
			LocateRegistry.createRegistry(6666);
			IBOOK bookutil = new IBOOKIMPL();
			Naming.bind("rmi://localhost:6666/BOOKUtil", bookutil);
            System.out.println("------------远程对象BOOKUtil注册成功,等待客户端调用...");		
		} catch (RemoteException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}

2.客户端
CLIENT.java

package RMIclient;

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

import RMIserve.IBOOK;



public class CLIENT {
	
	public static void main(String[] args) {
		
		try {
			IBOOK bookutil =(IBOOK) Naming.lookup("rmi://localhost:6666/BOOKUtil");
			util.setBookutil(bookutil);
			System.out.println("---------------欢迎使用书籍信息管理系统---------------");
			Scanner input = new Scanner(System.in);
			int choose  = -1;
			while(true) {
				System.out.println("选择菜单:");
				System.out.println("添加一个书籍对象:  1");
				System.out.println("查询指定ID号的书籍对象:2");
				System.out.println("按书名查询书籍对象列表:3");
				System.out.println("删除指定ID号的书籍对象: 4");
				System.out.println(" 退出系统:   0");
				
				System.out.println("请输入你所要进行的操作对应序号");	
				choose = input.nextInt();
				switch (choose) {
				case 1:
					util.add();
					break;
				case 2:
					util.queryByID();
					break;
				case 3:
					util.queryByname();
					break;
				case 4:
					util.delete();
					break;
				default:
					System.out.println("输入无效");
					break;
				}
				
			}
		} catch (MalformedURLException | RemoteException | NotBoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}

util.java:

package RMIclient;

import java.rmi.RemoteException;
import java.util.LinkedList;
import java.util.Scanner;

import RMIserve.BOOK;
import RMIserve.IBOOK;

public class util {
	
	private static IBOOK bookutil;
	private static Scanner input = new Scanner(System.in);
	public static void add() {
		System.out.println("请输入添加书籍对象的名称:");
		String name = input.nextLine();
		BOOK book = new BOOK(name);
		boolean b = false;
		try {
		     b =  bookutil.add(book);
		} catch (RemoteException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally {
			if(b==false) {
				System.out.println("添加失败");	
			}
			else {
				System.out.println("添加成功");
			}
		}
		
	}


	public static void queryByID() {
		System.out.println("请输入查询的ID");
		int id = input.nextInt();
		BOOK book = null;
		try {
			 book = bookutil.queryByID(id);
		} catch (RemoteException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		if(book==null) {
			System.out.println("没有此书");
		}
		else {
			System.out.println("该书信息为:");
			System.out.println(book.toString());
		}
	}
	
	public static void queryByname() {
		System.out.println("请输入查询书籍的名称:");
		String name = input.nextLine();
		LinkedList<BOOK> booklist = new	LinkedList<BOOK>();
		try {
				booklist = bookutil.queryByName(name);
		} catch (RemoteException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		if(booklist.size()==0) {
			System.out.println("没有该名称的书");
		}else {
			System.out.println("对应书籍信息如下:");
			for(BOOK book:booklist) {
				System.out.println(book.toString());
			}
		}

	}

	public static void delete() {
		System.out.println("请输入查询的ID");
		int id = input.nextInt();
		boolean b = false;
		try {
			b = bookutil.delete(id);
		} catch (RemoteException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally {
			if(b==false) {
				System.out.println("删除失败");
			}else {
				System.out.println("删除成功");
			}
		}
	}

	public static void setBookutil(IBOOK bookutil) {
		util.bookutil = bookutil;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值