赞
踩
crylearner专栏 一个简单的RPC框架是如何炼成的 :http://blog.csdn.net/crylearner/article/details/46898537
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
-
- import javax.swing.JOptionPane;
-
- import com.sun.corba.se.impl.legacy.connection.SocketFactoryContactInfoListImpl;
-
-
- public class Server {
-
- // 基于socket实现传输层
- public static void main(String[] args) {
- mServer server = new mServer();
-
- //server监听的端口号
- //port取值范围1-65535,这个server监听的端口号,一般指定较大的数值,这样可以和系统预留的端口号分开
- int port=12345;
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- Socket socket = serverSocket.accept();
- //提示建立链接
- JOptionPane.showMessageDialog(null, "有客户端链接到本机的12345端口");
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //while(true)
- {
- String msgFromClient=in.readLine();
- System.out.println("Server print recived msg:"+msgFromClient);
- String reqStr = msgFromClient;
- String rspStr = server.procRequest(reqStr);
- PrintWriter out = new PrintWriter(socket.getOutputStream());
- out.println(rspStr);
- out.flush();
- }
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /*
- class mResponse
- {
- int id;
- String result;
- String getResponse()
- {
- return "id"+id+"result"+result;
- }
- }
- class mRequest
- {
- int id;
- String command;
- int arg1;
- int arg2;
- String getRequest()
- {
- return "req:id"+id+"command"+command+"arg1"+arg1+"arg2"+arg2;
- }
- }
- */
-
- class mServer
- {
- // Server解析收到的Request报文(报文由id和command组成)
- // 将回复报文id设置为收到的报文一致
- // 如果command为sayHello,那么范围的报文内容为Hello
- // 如果为其他命令,我们暂时没有设定,所有输出无法解析提示消息unknown command
- String procRequest(String reqStr)
- {
-
- //return "Hello World";
- mRequest req =decode(reqStr);
- mResponse rsp = new mResponse();
- rsp.id = req.id;
- if(req.command.equals("sayHello"))
- {
- rsp.result = "Hello";
- }else if(req.command.equals("add"))
- {
- rsp.result = Integer.toString(req.arg1+req.arg2);
- }
- else
- {
- System.out.println("req command"+req.command);
- rsp.result = "unknown command";
- }
- return encode(rsp);
- }
- // rspStr格式:rsp:id+@id+result+@result
- String encode(mResponse response)
- {
- String rspStr;
- rspStr="rsp:id"+response.id+"result"+response.result;
- return rspStr;
- }
- // reqStr格式:req:id+@id+command+@command+arg1+@arg1+arg2+@arg2
- mRequest decode(String reqStr)
- {
- //System.out.println("serverdecode reqStr:"+reqStr);
- mRequest request = new mRequest();
- if(!reqStr.startsWith("req:"))
- {
- System.err.println("it is not a request data");
- }else
- {
- request.id=Integer.parseInt(reqStr.substring(reqStr.indexOf("id")+2,reqStr.indexOf("command")));
- request.command=reqStr.substring(reqStr.indexOf("command")+7, reqStr.indexOf("arg1"));
- //System.out.println("server decode command:"+request.command);
- request.arg1=Integer.parseInt(reqStr.substring(reqStr.indexOf("arg1")+4, reqStr.indexOf("arg2")));
- request.arg2=Integer.parseInt(reqStr.substring(reqStr.indexOf("arg2")+4));
- }
- return request;
- }
- }
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
-
- public class Client {
- public static void main(String[] args) {
- mClient client = new mClient();
- client.setReqId(1);
- client.setReqCommand("sayHello");
- String reqStr = client.req.getRequest();
- try {
- Socket socket = new Socket("localhost",12345);
- PrintWriter out = new PrintWriter(socket.getOutputStream());
- //将reqStr传给server
- out.println(reqStr);
- out.flush();
-
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //while()
- {
- String rspStr = in.readLine();
- System.out.println("response form Server:"+rspStr);
- client.rsp = client.decode(rspStr);
- System.out.println("decode result:"+client.rsp.result);
- }
- //BufferedReader in = new BufferedReader(in)
- //socket.close();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- class mResponse
- {
- int id;
- String result;
- String getResponse()
- {
- return "id"+id+"result"+result;
- }
- }
- class mRequest
- {
- int id;
- String command;
- int arg1;
- int arg2;
- String getRequest()
- {
- return "req:id"+id+"command"+command+"arg1"+arg1+"arg2"+arg2;
- }
- }
- class mClient{
-
- mServer remote;
- mRequest req = new mRequest();
- String reqStr = req.getRequest();
- mResponse rsp = new mResponse();
- void setReqId(int id)
- {
- req.id=id;
- }
- void setReqCommand(String command)
- {
- req.command=command;
- }
- mClient()
- {
- remote = null;
- }
-
- String getRsp(String reqStr)
- {
- String rspStr = remote.procRequest(reqStr);
- mResponse rsp=decode(rspStr);
- return rsp.result;
- }
- String sayHello()
- {
- req.id=1;
- req.command="sayHello";
- //req.getRequest()
- return getRsp(req.getRequest());
- }
- String add(int a,int b)
- {
- req.id=1;
- req.command="add";
- req.arg1=a;
- req.arg2=b;
- return getRsp(req.getRequest());
- }
- mResponse decode(String rspStr)
- {
- mResponse response = new mResponse();
- if(!rspStr.startsWith("rsp:"))
- {
- System.out.println("it is not a response data");
- }else{
- response.id=Integer.parseInt(rspStr.substring(rspStr.indexOf("id")+2, rspStr.indexOf("result")));
- response.result=rspStr.substring(rspStr.indexOf("result")+6);
- }
- return response;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。