1. 程式人生 > >RMI IP繫結和埠固定

RMI IP繫結和埠固定

這裡描述的是RMI服務端怎麼繫結到IP和固定埠。

當機器有多個IP,並且防火牆要求固定服務埠時就有用了

這個過程包括了RMI的基本典型應用,既有服務端的遠端物件匯出也有客戶端的遠端物件匯出.完整地固定了兩段的IP和埠.

原理是在建立遠端物件登錄檔和匯出遠端物件時使用自己寫的SocketFactory來

建立Socket和ServerSocket。

1.寫個類,實現RMIClientSocketFactory和RMIServerSocketFactory介面.

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;

public class StaticRmiSocketFactory implements RMIClientSocketFactory,
  RMIServerSocketFactory, Serializable
{
 
 private static final long serialVersionUID = 1L;
 private String staticIp;
 private int staticPort;

 public StaticRmiSocketFactory(String ip, int port)
 {
  this.staticIp = ip;
  this.staticPort = port;
 }

 public Socket createSocket(String ip, int port) throws IOException
 {
  System.out.println("create client socket " + this.staticIp + ":" + this.staticPort);
  return new Socket(this.staticIp, this.staticPort);
 }

 public ServerSocket createServerSocket(int port) throws IOException
 {
  System.out.println("create server socket " + this.staticIp + ":" + this.staticPort);
  return new ServerSocket(this.staticPort, 0, InetAddress
    .getByName(this.staticIp));
 }

}

2. 服務介面和服務實現類:

服務介面:

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


public interface ServerService extends Remote
{
 public void serverMethod(ClientService cs) throws RemoteException;

}

服務實現類:
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;


public class SomeServiceImpl extends UnicastRemoteObject implements ServerService
{

 
 private static final long serialVersionUID = 1L;

 protected SomeServiceImpl(int port, RMIClientSocketFactory csf,
   RMIServerSocketFactory ssf) throws RemoteException
 {
  super(port, csf, ssf);
 }

 public void serverMethod(ClientService cs) throws RemoteException
 {
  String ip = cs.clientMethod("server method send value");
  System.out.println("server method print ++ client method return is :" + ip);
  
 }

}

3.RMI服務端繫結過程:

 import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class Server
{

 
 public static void main(String[] args) throws RemoteException, AlreadyBoundException
 {
  String serverIp = "192.168.0.107";
  
  String regIP = serverIp;
  int regPort = 10001;
  
  StaticRmiSocketFactory regFac = new StaticRmiSocketFactory(regIP, regPort);  
  Registry reg = LocateRegistry.createRegistry(regPort, regFac, regFac);
  
  
  String exportIp = serverIp;
  int exportPort = 10002;
  StaticRmiSocketFactory exportFac = new StaticRmiSocketFactory(exportIp, exportPort);
  ServerService ss = new SomeServiceImpl(exportPort, exportFac, exportFac);
  
  
  reg.bind("SomeService", ss);
  while(true)
  {
   try
   {
    Thread.sleep(10000);
   } catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }
  

 }

}

4.客戶端遠端方法介面
 import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;


public interface ClientService extends Remote, Serializable
{
 public String clientMethod(String a) throws RemoteException;

}

5. 客戶端遠端方法介面實現

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

public class ClientServiceImpl extends UnicastRemoteObject implements
  ClientService
{

 protected ClientServiceImpl(int arg0, RMIClientSocketFactory arg1,
   RMIServerSocketFactory arg2) throws RemoteException
 {
  super(arg0, arg1, arg2);
 }

 
 private static final long serialVersionUID = 1L;

 public String clientMethod(String a) throws RemoteException
 {
  System.out.println("client method arg value:" + a);
  return "client method return value";
 }

}

6.RMI客戶端繫結過程:

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Client
{

 
 public static void main(String[] args) throws MalformedURLException,
   RemoteException, NotBoundException
 {
  String ServerIP = "192.168.0.107";
  int serverRegPort = 10001;

  String clientIP = "127.0.0.1";
  int clientPort = 10003;

  StaticRmiSocketFactory expFac = new StaticRmiSocketFactory(clientIP,
    clientPort);

  ClientService cs = new ClientServiceImpl(0, expFac, expFac);

  String url = "rmi://" + ServerIP + ":" + serverRegPort + "/SomeService";
  ServerService ss = (ServerService) Naming.lookup(url);
  ss.serverMethod(cs);
  while (true)
  {
   try
   {
    Thread.sleep(10000);
   } catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }

 }

}