利用socket进行远程过程调用

  • ServiceServer 服务端,一开始启动这个
package com.huachao.socket;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServiceServer {
    public static void main(String[] args) throws Exception {
        //创建一个ServerSocket
        ServerSocket server = new ServerSocket();
        //绑定到本机的8899端口
        server.bind(new InetSocketAddress("localhost", 8899));
        //接收客户端请求
        //accept是一个阻塞方法,会一直等待,到有客户端请求连接才返回
        
        
        while(true){
            Socket socket = server.accept();
            new Thread(new ServiceServerTask(socket)).start();
        }
        
    }
}
  • 子线程,调用getData,返回调用结果
package com.huachao.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ServiceServerTask implements Runnable{
    private Socket socket;
    public ServiceServerTask(Socket socket) {
        this.socket = socket;
    }
    //业务逻辑:跟客户端进行数据交互
    @Override
    public void run() {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = socket.getInputStream();
            out = socket.getOutputStream();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out);
            
            
            //从网络通信输入流中读取客户端发送过来的数据
            //注意:socketinputstream的读数据的方法都是阻塞的
            String param = br.readLine();
            /**
             * 作业:
             * 将以下业务调用逻辑写成更加通用的:可以根据客户端发过来的调用类名、调用方法名、调用该参数来灵活调用
             * 
             * 《反射》
             * 
             */
            GetDataServiceImpl impl = new GetDataServiceImpl();
            String result = impl.getData(param);
            
            
            pw.println(result);
            pw.flush();
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
                out.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
}
  • 被远程调用的方法
package com.huachao.socket;

public class GetDataServiceImpl {
    public String getData(String param){
        return "ok-"+param;
    }
}
  • ServiceClient 客户端
package com.huachao.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ServiceClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 8899);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        
        PrintWriter pw = new PrintWriter(out);
        pw.println("hello");
        pw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String result = br.readLine();
        System.out.println(result);
        
        in.close();
        out.close();
        socket.close();
        
    }
}

你可能感兴趣的:(利用socket进行远程过程调用)