Á¦¸ñ : rmiÀÇ ±âº»
// Hello.java
// remote interface
public interface Hello extends java.rmi.Remote {
String sayHello() throws java.rmi.RemoteException;
}
// HelloImpl.java
// interface implement + server
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl
extends UnicastRemoteObject
implements Hello
{
private String name;
public HelloImpl(String s) throws java.rmi.RemoteException {
super();
name = s;
}
public String sayHello() throws RemoteException {
return "Hello World!";
}
public static void main(String args[])
{
// Create and install the security manager
System.setSecurityManager(new RMISecurityManager());
try {
HelloImpl obj = new HelloImpl("HelloServer");
// Naming.rebind("//host-name/rmi-nameHelloServer", obj);
Naming.rebind("//www.sbr.net/HelloServer", obj);
System.out.println("HelloImpl created and bound in the registry to the name HelloServer");
} catch (Exception e) {
System.out.println("HelloImpl.main: an exception occurred:");
e.printStackTrace();
}
}
}
// HelloClient.java
// client
import java.rmi.*;
public class HelloClient {
public static void main(String args[])
{
String message = "";
Hello obj = null;
try {
System.setSecurityManager(new RMISecurityManager());
// obj = (Hello)Naming.lookup("//host-name/rmi-name");
obj = (Hello)Naming.lookup("//www.sbr.net/HelloServer");
} catch (Exception e) {
System.out.println("1");
e.printStackTrace();
}
try {
message = obj.sayHello();
} catch (Exception e) {
System.out.println("HelloApplet: an exception occurred:");
e.printStackTrace();
}finally{
System.out.println(message);
}
}
}
¼³¸í:
1. ÆÄÀϱ¸¼º
clientÃøÀÇ ÇÊ¿äÈÀÏ.
Hello.java, HelloClient.java
ServerÃøÀÇ ÇÊ¿äÈÀÏ.
Hello.java, HelloImpl.java
2.ServerÃø ÁøÇà°úÁ¤.
1) javac HelloImpl.java
2) unset CLASSPATH
unsetenv CLASSPATH
rmiregistry &
3) rmic HelloImpl
4) java -Djava.rmi.server.codebase=http://url/ HelloImpl &
ex)
java -Djava.rmi.server.codebase=http://www.sbr.net/test/ HelloImpl &
ÁÖÀÇ) test´ÙÀ½¿¡ / ¸¦ ²À »ç¿ëÇØ¾ßÇÑ´Ù.
3.ClientÃø ÁøÇà°úÁ¤.
1) javac HelloClient.java
2) java HelloClient
Âü°í) À§ÀÇ ³»¿ëÀº jdk1.1.5 doc¿¡ ÀÖ´ø ³»¿ëÀ» ÇѱÛÈ ÇÑ ³»¿ëÀÔ´Ï´Ù.
|