RMI学习

接口
import java.rmi.Remote; 
import java.rmi.RemoteException; 

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

它定义了一个方法,sayHello,实现向调用者返回一个字符串Server类
import java.rmi.registry.Resistry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

public class Server implements Hello 
{ 
  public Server(){} 
  public String sayHello() 
   { 
    return  “Hello,World!”; 
   } 
  public static void main(String args[]) 
   { 
    Try{ 
        Server obj=new Server (); 
        Hello stub=(Hello)UnicastRemoteObject.explortObject(obj,0); 
        //Bind the remote object’ s stub in the registry 
        Registry registry=LocateRegistry.getRegistry(); 
        Registry.bind(“Hello”,stub); 
        System.err.println(“Server ready”); 
       }catch(Exception e) 
        { 
         System.err.println(“Server exception:”+e.toString()); 
         e.printStackTrace(); 
        } 
    } 
}


 
 
接口
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ReceiveMessageInterface extends Remote
{
	String receiveMessage(String x) throws RemoteException;
}

server端
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiServer extends java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface
{
	int thisPort;
	String thisAddress;
	Registry registry; // rmi registry for lookup the remote objects.
	// This method is called from the remote client by the RMI.
	// This is the implementation of the 锟斤拷ReceiveMessageInterface锟斤拷.
	public String receiveMessage(String x) throws RemoteException
	{
		System.out.println(x);
		return "Got " +x;
	}
	public RmiServer() throws RemoteException, MalformedURLException
	{
		try
		{
			// get the address of this host.
			thisAddress = (InetAddress.getLocalHost()).toString();
		}catch (Exception e)
		{
			throw new RemoteException("can't get inet address.");
		}
		thisPort = 3232; // this port(registry锟斤拷s port)
		System.out.println("server address=" + thisAddress + ",port=" + thisPort);
		try
		{
			// create the registry and bind the name and object.
			registry = LocateRegistry.createRegistry(thisPort);
			registry.rebind("rmiServer", this);
			//Naming.rebind("rmiServer", this);
		}catch (RemoteException e)
		{
			throw e;
		}
		//System.out.println(this.getRef());
	}
	static public void main(String args[])
	{
		try
		{
			RmiServer s = new RmiServer();
		}catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}
	}
}

client端
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiClient
{
	static public void main(String args[])
	{
		ReceiveMessageInterface rmiServer;
		Registry registry;
		String serverAddress = "127.0.0.1";//args[0];
		String serverPort = "3232";//args[1];
		String text = "Hi rmi!";//args[2];
		System.out.println("sending " + text + " to " + serverAddress + ":"+ serverPort);
		try
		{
			// get the 锟斤拷registry锟斤拷
			registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue());
			// look up the remote object
			rmiServer = (ReceiveMessageInterface) (registry.lookup("rmiServer"));
			// call the remote method
			System.out.println(rmiServer.receiveMessage(text));
		} catch (RemoteException e)
		{
			e.printStackTrace();
		} catch (NotBoundException e)
		{
			e.printStackTrace();
		}
	}
}

另外,对于多个对象我们可以指定为相同的端口也可以指定为不同的端口,只要保证绑定的key指唯一就可以了。当然在服务器端指定端口后,启动RMI注册服务和客户端进行lookup时要与服务器指定的端口相同。一个端口一个registry。
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值