lightbulbclient.java
来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 57 行
JAVA
57 行
import java.rmi.*;
// Chapter 11, Listing 4
public class LightBulbClient
{
public static void main(String args[])
{
System.out.println ("Looking for light bulb service");
try
{
// Check to see if a registry was specified
String registry = "localhost";
if (args.length >=1)
{
registry = args[0];
}
// Registration format //registry_hostname (optional):port /service
String registration = "rmi://" + registry + "/RMILightBulb";
// Lookup the service in the registry, and obtain a remote service
Remote remoteService = Naming.lookup ( registration );
// Cast to a RMILightBulb interface
RMILightBulb bulbService = (RMILightBulb) remoteService;
// Turn it on
System.out.println ("Invoking bulbservice.on()");
bulbService.on();
// See if bulb has changed
System.out.println ("Bulb state : " + bulbService.isOn() );
// Conserve power
System.out.println ("Invoking bulbservice.off()");
bulbService.off();
// See if bulb has changed
System.out.println ("Bulb state : " + bulbService.isOn() );
}
catch (NotBoundException nbe)
{
System.out.println ("No light bulb service available in registry!");
}
catch (RemoteException re)
{
System.out.println ("RMI Error - " + re);
}
catch (Exception e)
{
System.out.println ("Error - " + e);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?