📄 ch11.txt
字号:
/* 代码11-1
* Created on 2005-5-22
*/
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
interface AccurateTimeI extends Remote {
long getAccurateTime() throws RemoteException;
}//它对Remote进行了扩展,而且它的所有方法都会"掷"出RemoteException(远程违例)。
public class AccurateTime
extends UnicastRemoteObject
implements AccurateTimeI {
// 从远程接口继承出来
public long getAccurateTime()
throws RemoteException {
return System.currentTimeMillis();
}
// 注意必须要实现构造函数以便抛出异常,即使是默认函数
public AccurateTime() throws RemoteException {
super();
}
public static void main(String[] args)
throws Exception {
System.setSecurityManager(
new RMISecurityManager());
AccurateTime pt = new AccurateTime();
Naming.bind(
"2005/AccurateTime", pt);
System.out.println("Ready to do time");
}
}
/* 代码11-2
* Created on 2005-5-22
*/
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FileInterface extends Remote {
public byte[] downloadFile(String fileName) throws
RemoteException;
}
/* 代码11-3
* Created on 2005-5-22
*/
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FileImpl extends UnicastRemoteObject
implements FileInterface {
private String name;
public FileImpl(String s) throws RemoteException{
super();//初始化父类
name = s;
}
public byte[] downloadFile(String fileName){
try {
File file = new File(fileName);
byte buffer[] = new byte[(int)file.length()];//获得字节数组
BufferedInputStream input = new
BufferedInputStream(new FileInputStream(fileName));
input.read(buffer,0,buffer.length);//读入缓存
input.close();//关闭输入流
return(buffer);//返回数组
} catch(Exception e){
System.out.println("FileImpl: "+e.getMessage());
e.printStackTrace();
return(null);
}
}
}
/* 代码11-4
* Created on 2005-5-22
*/
import java.io.*;
import java.rmi.*;
public class FileServer {
public static void main(String argv[]) {
if(System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());//设置安全管理器
}
try {
FileInterface fi = new FileImpl("FileServer");
Naming.rebind("//127.0.0.1/FileServer", fi);
} catch(Exception e) {
System.out.println("FileServer: "+e.getMessage());//打印错误信息
e.printStackTrace();
}
}
}
/* 代码11-5
* Created on 2005-5-22
*/
import java.io.*;
import java.rmi.*;
public class FileClient{
public static void main(String argv[]) {
if(argv.length != 2) {
System.out.println("Usage: java FileClient fileName machineName");
System.exit(0);//退出系统
}
try {
String name = "//" + argv[1] + "/FileServer";
FileInterface fi = (FileInterface) Naming.lookup(name);
byte[] filedata = fi.downloadFile(argv[0]);
File file = new File(argv[0]);
BufferedOutputStream output = new
BufferedOutputStream(new FileOutputStream(file.getName()));//通过缓冲方式输出数据
output.write(filedata,0,filedata.length);
output.flush();
output.close();//关闭输出流
} catch(Exception e) {
System.err.println("FileServer exception: "+ e.getMessage());
e.printStackTrace();
}
}
}
/* 代码11-6
* Created on 2005-5-22
*/
class BookImpl extends ProductImpl
{
public BookImpl(String title, String theISBN,
int sex, int age1, int age2, String hobby)
{
super(title + " Book", sex, age1, age2, hobby);
ISBN = theISBN;
}
public String getStockCode() { return ISBN; }
private String ISBN;
}
/* 代码11-7
* Created on 2005-5-22
*/
interface StockUnit extends Remote
{
public String getStockCode() throws RemoteException;
}
class BookImpl extends ProductImpl implements StockUnit
{
public BookImpl(String title, String theISBN,
int sex, int age1, int age2, String hobby)
throws RemoteException
{
super(title + " Book", sex, age1, age2, hobby);
ISBN = theISBN;
}
public String getStockCode() throws RemoteException
{
return ISBN;
}
private String ISBN;
}
/* 代码11-8
* Created on 2005-5-22
*/
interface Product extends Remote
{
public Object remoteClone()
throws RemoteException, CloneNotSupportedException;
}
class ProductImpl extends UnicastRemoteObject
implements Product
{
public Object remoteClone()
throws CloneNotSupportedException
{ return clone(); }
}
/* 代码11-9
* Created on 2005-5-22
*/
package java.rmi;
public final class Naming
{
public static Remote lookup(String url)
throws NotBoundException, java.net.MalformedURLException, RemoteExceptioon;
public static void bind(String url, Remote obj)
throws AlreadyBoundException, java.net.MalformedURLException, RemoteExceeption;
public static void rebind(String url, Remote obj)
throws RemoteException, java.net.MalformedURLException;
public static void unbind(String url)
throws RemoteException, NotBoundException, java.net.MalformedURLExceptioon;
public static String[] list(String url)
throws RemoteException, java.net.MalformedURLException;
}
/* 代码11-10
* Created on 2005-5-22
*/
import java.rmi.*;
public class RegistryLister {
public static void main(String[] args) {
int port = 1099;
if (args.length == 0) {
System.err.println("Usage: java RegistryLister host port");//打印错误信息
return;
}
String host = args[0];
if (args.length > 1) {
try {
port = Integer.parseInt(args[1]);
if (port <1 || port > 65535) port = 1099;//设置端口号
}
catch (NumberFormatException e) {}
}
String url = "rmi://" + host + ":" + port + "/";
try {
String[] remoteObjects = Naming.list(url);
for (int i = 0; i < remoteObjects.length; i++) {
System.out.println(remoteObjects[i]);
}
}
catch (RemoteException e) {
System.err.println(e);//打印错误信息
}
catch (java.net.MalformedURLException e) {
System.err.println(e); //打印错误信息
}
}
}
/* 代码11-11
* Created on 2005-5-22
*/
import ChinaApp.*;
import org.omg.CosNaming.*;
mport org.omg.CORBA.*;
public class ChinaClient {
public static void main(String args[]){
try {
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("China", "");
NameComponent path[]= {nc};
China ChinaRef = ChinaHelper.narrow(ncRef.resolve(path));
String China = ChinaRef.MorningChina();
System.out.println(China);
} catch(Exception e) {
System.out.println("ERROR : " + e); e.printStackTrace(System.out);
}
} // main()
} // ChinaClient
/* 代码11-12
* Created on 2005-5-22
*/
import ChinaApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class ChinaServer
{
public static void main(String args[]){}
try{
ORB orb = ORB.init(args, null);
ChinaServant ChinaRef = new ChinaServant();
orb.connect(ChinaRef);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("China", "");
NameComponent path[]= {nc};
ncRef.rebind(path, ChinaRef);
java.lang.Object sync = new java.lang.Object();
synchronized(sync){ sync.wait(); }
} catch(Exception e) {
System.out.println("ERROR: " + e);
e.printStackTrace(System.out);
}
} // main()
// ChinaServer
class ChinaServant extends _ChinaImplBase{
public String MorningChina() { return "\n\n中国,早上好!\n\n; }
}
/* 代码11-13
* Created on 2005-5-22
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -