longgangbai 发表于 2013-1-15 03:00:07

远程技术的研究

客户端访问的代理类的应用开发
package cn.com.huawei.socket.remote.rpc;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
 * 远程服务的客户端应用
 * @author bailonggang
 * 2009-2-21
 * 下午05:50:44
 */
public class RemoteClientProxyFactory {
 private int port;
 private String hostname;
 //创建一个服务对象
 private Call  call;
 public  RemoteClientProxyFactory(int port,String hostname,Call  call)
 {
  this.port=port;
  this.hostname=hostname;
  this.call=call;
 }
  public void invoke()throws Exception{
   //创建一个客户端
   Socket socket=new Socket(hostname,port);
   //获取服务端对象和发送对象
   OutputStream os=socket.getOutputStream();
   InputStream is=socket.getInputStream();
   ObjectOutputStream oos=new ObjectOutputStream(os);
   ObjectInputStream ois=new ObjectInputStream(is);
   //new Call("cn.com.huawei.socket.commons.HelloService","echo",new Class[]{String.class},new Object[]{"hello"});
      //向服务器发送Call对象
   oos.writeObject(call);
   //读取服务器返回的对象Call中包含返回的结果
       call=(Call)ois.readObject();
       System.out.println(call.getResult());
       ois.close();
       oos.close();
       is.close();
       os.close();
  }
}
页: [1]
查看完整版本: 远程技术的研究