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

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();        
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值