简单的RPC(未用动态代理)

来源:互联网 发布:招商证券理财软件下载 编辑:程序博客网 时间:2024/06/05 14:06
package app01;import java.io.Serializable;public class Didi implements Serializable{    public String name;    public int age;    public Didi() {        super();    }    public Didi(String name, int age) {        super();        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "Didi [name=" + name + ", age=" + age + "]";    }    public String getRpc(){        return "RPC的调用";    }    public String getAddress(String s){        return s;    }    public String getAddress(String s, int num){        return s + "  地铁: " + num;    }}

//———————–华丽的分割线———————————

package app01;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.List;public class Server {    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InstantiationException {            ServerSocket server = new ServerSocket();            server.bind(new InetSocketAddress(8089));               while(true){                Socket socket = server.accept();                InputStream is = socket.getInputStream();                ObjectInputStream ois = new ObjectInputStream(is);                String className = ois.readUTF();                Class c = Class.forName(className);                             String methodName = ois.readUTF();                Class[] ps = (Class[]) ois.readObject();//参数类型                Method method = c.getMethod(methodName, ps);                   Object[] arguments  =  (Object[]) ois.readObject();//参数                String ms = null;                try {                    ms = (String) method.invoke(c.newInstance(), arguments);                                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());                    oos.writeObject(ms);                    oos.flush();                    oos.close();                } catch (InvocationTargetException e) {                    e.printStackTrace();                }            }    }}

//—————不太华丽的分割线————————

package app01;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.InetSocketAddress;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class Client <T>{    public static void main(String[] args) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException  {        RpcGetMes("app01.Didi", "getAddress", new Class[]{String.class, int.class}, new Object[]{"回龙观东大街", 8});    }    static void RpcGetMes(String className, String methodName, Class[] ps, Object[] paras)throws IOException, ClassNotFoundException{        Socket socket = new Socket();        socket.connect(new InetSocketAddress("localhost", 8089));        OutputStream os = socket.getOutputStream();        ObjectOutputStream oos = new ObjectOutputStream(os);        oos.writeUTF(className);        oos.writeUTF(methodName);        oos.writeObject(ps);        oos.writeObject(paras);        oos.flush();        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());        String reMes = (String) ois.readObject();        System.out.println("客户端收到数据: " + reMes);        oos.close();            }}
1 0