当前位置:   article > 正文

Java实现一个简单的RPC框架(五) 基于Socket的传输层实现_rvc实现数据传输java

rvc实现数据传输java

参考:

crylearner专栏  一个简单的RPC框架是如何炼成的 :http://blog.csdn.net/crylearner/article/details/46898537


前面我们已经是实现了本地功能调用、指定简单的协议并通过协议实现调用、带参数的本地功能调用、编码解码功能,但是我们的调用都是基于本地的,我们还没有实现远程调用,接下来我们来实现传输层。这里我们基于socket信,让调用方可以通过传输层将请求报文传送给Server,Server接受解析并处理后将结果通过传输层再送给client。

实现了传输层的功能,我们就实现了代码分离,我们的发送端不需要知道具体的Server端是如何处理我们穿过去的参数的,只需到等着Server向它传送结果就行了。
同样,Server端不需要知道请求端,是如何组织参数的,只需要将传过来的参数解析处理,并将结果送回请求端即可。
因此,我将我们之前写好的两个class,mClient和mServer分别放在Client.java和Server.java中,并添加socket通信代码。

Java代码实现:

为了便于调试,我将这两个程序写在了一个项目中,但是Client.java和Server.java都是包含main方法的可以单独运行的java程序。

Server.java
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import javax.swing.JOptionPane;
  8. import com.sun.corba.se.impl.legacy.connection.SocketFactoryContactInfoListImpl;
  9. public class Server {
  10. // 基于socket实现传输层
  11. public static void main(String[] args) {
  12. mServer server = new mServer();
  13. //server监听的端口号
  14. //port取值范围1-65535,这个server监听的端口号,一般指定较大的数值,这样可以和系统预留的端口号分开
  15. int port=12345;
  16. ServerSocket serverSocket = null;
  17. try {
  18. serverSocket = new ServerSocket(port);
  19. Socket socket = serverSocket.accept();
  20. //提示建立链接
  21. JOptionPane.showMessageDialog(null, "有客户端链接到本机的12345端口");
  22. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  23. //while(true)
  24. {
  25. String msgFromClient=in.readLine();
  26. System.out.println("Server print recived msg:"+msgFromClient);
  27. String reqStr = msgFromClient;
  28. String rspStr = server.procRequest(reqStr);
  29. PrintWriter out = new PrintWriter(socket.getOutputStream());
  30. out.println(rspStr);
  31. out.flush();
  32. }
  33. socket.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. /*
  40. class mResponse
  41. {
  42. int id;
  43. String result;
  44. String getResponse()
  45. {
  46. return "id"+id+"result"+result;
  47. }
  48. }
  49. class mRequest
  50. {
  51. int id;
  52. String command;
  53. int arg1;
  54. int arg2;
  55. String getRequest()
  56. {
  57. return "req:id"+id+"command"+command+"arg1"+arg1+"arg2"+arg2;
  58. }
  59. }
  60. */
  61. class mServer
  62. {
  63. // Server解析收到的Request报文(报文由id和command组成)
  64. // 将回复报文id设置为收到的报文一致
  65. // 如果command为sayHello,那么范围的报文内容为Hello
  66. // 如果为其他命令,我们暂时没有设定,所有输出无法解析提示消息unknown command
  67. String procRequest(String reqStr)
  68. {
  69. //return "Hello World";
  70. mRequest req =decode(reqStr);
  71. mResponse rsp = new mResponse();
  72. rsp.id = req.id;
  73. if(req.command.equals("sayHello"))
  74. {
  75. rsp.result = "Hello";
  76. }else if(req.command.equals("add"))
  77. {
  78. rsp.result = Integer.toString(req.arg1+req.arg2);
  79. }
  80. else
  81. {
  82. System.out.println("req command"+req.command);
  83. rsp.result = "unknown command";
  84. }
  85. return encode(rsp);
  86. }
  87. // rspStr格式:rsp:id+@id+result+@result
  88. String encode(mResponse response)
  89. {
  90. String rspStr;
  91. rspStr="rsp:id"+response.id+"result"+response.result;
  92. return rspStr;
  93. }
  94. // reqStr格式:req:id+@id+command+@command+arg1+@arg1+arg2+@arg2
  95. mRequest decode(String reqStr)
  96. {
  97. //System.out.println("serverdecode reqStr:"+reqStr);
  98. mRequest request = new mRequest();
  99. if(!reqStr.startsWith("req:"))
  100. {
  101. System.err.println("it is not a request data");
  102. }else
  103. {
  104. request.id=Integer.parseInt(reqStr.substring(reqStr.indexOf("id")+2,reqStr.indexOf("command")));
  105. request.command=reqStr.substring(reqStr.indexOf("command")+7, reqStr.indexOf("arg1"));
  106. //System.out.println("server decode command:"+request.command);
  107. request.arg1=Integer.parseInt(reqStr.substring(reqStr.indexOf("arg1")+4, reqStr.indexOf("arg2")));
  108. request.arg2=Integer.parseInt(reqStr.substring(reqStr.indexOf("arg2")+4));
  109. }
  110. return request;
  111. }
  112. }

Client.java
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. public class Client {
  9. public static void main(String[] args) {
  10. mClient client = new mClient();
  11. client.setReqId(1);
  12. client.setReqCommand("sayHello");
  13. String reqStr = client.req.getRequest();
  14. try {
  15. Socket socket = new Socket("localhost",12345);
  16. PrintWriter out = new PrintWriter(socket.getOutputStream());
  17. //将reqStr传给server
  18. out.println(reqStr);
  19. out.flush();
  20. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  21. //while()
  22. {
  23. String rspStr = in.readLine();
  24. System.out.println("response form Server:"+rspStr);
  25. client.rsp = client.decode(rspStr);
  26. System.out.println("decode result:"+client.rsp.result);
  27. }
  28. //BufferedReader in = new BufferedReader(in)
  29. //socket.close();
  30. } catch (UnknownHostException e) {
  31. e.printStackTrace();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. class mResponse
  38. {
  39. int id;
  40. String result;
  41. String getResponse()
  42. {
  43. return "id"+id+"result"+result;
  44. }
  45. }
  46. class mRequest
  47. {
  48. int id;
  49. String command;
  50. int arg1;
  51. int arg2;
  52. String getRequest()
  53. {
  54. return "req:id"+id+"command"+command+"arg1"+arg1+"arg2"+arg2;
  55. }
  56. }
  57. class mClient{
  58. mServer remote;
  59. mRequest req = new mRequest();
  60. String reqStr = req.getRequest();
  61. mResponse rsp = new mResponse();
  62. void setReqId(int id)
  63. {
  64. req.id=id;
  65. }
  66. void setReqCommand(String command)
  67. {
  68. req.command=command;
  69. }
  70. mClient()
  71. {
  72. remote = null;
  73. }
  74. String getRsp(String reqStr)
  75. {
  76. String rspStr = remote.procRequest(reqStr);
  77. mResponse rsp=decode(rspStr);
  78. return rsp.result;
  79. }
  80. String sayHello()
  81. {
  82. req.id=1;
  83. req.command="sayHello";
  84. //req.getRequest()
  85. return getRsp(req.getRequest());
  86. }
  87. String add(int a,int b)
  88. {
  89. req.id=1;
  90. req.command="add";
  91. req.arg1=a;
  92. req.arg2=b;
  93. return getRsp(req.getRequest());
  94. }
  95. mResponse decode(String rspStr)
  96. {
  97. mResponse response = new mResponse();
  98. if(!rspStr.startsWith("rsp:"))
  99. {
  100. System.out.println("it is not a response data");
  101. }else{
  102. response.id=Integer.parseInt(rspStr.substring(rspStr.indexOf("id")+2, rspStr.indexOf("result")));
  103. response.result=rspStr.substring(rspStr.indexOf("result")+6);
  104. }
  105. return response;
  106. }
  107. }

运行一下:
先运行Server.java启动ServerSocket等待客户端请求,再运行Client.java通过socket传送请求报文,服务器接受解析处理后将结果再通过socket传给请求端,请求端解析后得到结果。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/379903
推荐阅读
相关标签
  

闽ICP备14008679号