Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Помогите с rmi


Автор: muzikka1 14.6.2004, 14:57
Буду очень благодарен, курсовой стоит.

Автор: Domestic Cat 14.6.2004, 15:38
Interface Hello:

Код

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

public interface Hello extends Remote
{
   public String helloWorld () throws RemoteException;
}


class HelloImpl

Код

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

public class HelloImpl extends UnicastRemoteObject implements Hello
{
   public HelloImpl() throws RemoteException
   {
       // empty but must throw RemoteException
   }
   
   public String helloWorld () throws RemoteException
   {
           return "Hello world";
   }
}



class Server
Код

import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;

public class Server
{
   private static final String SERVER_NAME = "Server";
   private static final String HOST = "localhost";
   
   private String  port;
   
   public Server(String port)    
   {
       this.port = port;
   }
   
   public void runServer() throws  IllegalArgumentException,
                                   NotBoundException,
                                   RemoteException
   {
       HelloImpl helloImpl = new HelloImpl();
       Registry registry = LocateRegistry.createRegistry(Integer.parseInt(port, 10));
       String url = "//" + HOST + ":" + port + "/" + SERVER_NAME;
       registry.rebind(SERVER_NAME, helloImpl);
   }
}


Class Start

Код

import java.rmi.Naming;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;

public class Start
{
   public static final String SERVER_NAME = "Server";
   // use : server <port> - start server
   // client <host> <port> - start client
   public static void main (String[] args) throws Exception  
   {
       if (args[0].equals("server"))
       {
           Server server = new Server(args[1]);
           server.runServer();
           System.out.println("Server started on port " + args[1]);
       }
       else
       {
           String lookupString = "//" + args[1] + ":" + args[2] + "/" + SERVER_NAME;
           Hello hello = (Hello) Naming.lookup(lookupString);
           System.out.println("RMI object found");
           System.out.println(hello.helloWorld());
       }
   }
}


1. javac *.java
2. rmic HelloImpl (poluchatsa 2 faila - _stub i _skel)
3. Zapustit' server: java Start server 2000 // lyuboy port > 1023
4. Vo vtoroy JVM zapustit' clienta:
java Start client xxx.xxx.xxx.xxx 2000 // vash host

Uchtite, chto cherez firewall eto rabotat' ne budet (skoree vsego) esli ne otkryt' sootvetstv. port.

Автор: muzikka1 14.6.2004, 16:25
Спа-си-бо!

Автор: Sun 15.6.2004, 10:34
Пример от Domestic Cat включен в FAQ. Domestic Cat награжден очередным повышением репутации smile.gif

Автор: Domestic Cat 15.6.2004, 15:11
Spasiba smile.gif adv/jump.gif


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)