网络Networks
协议Protocol
是什么
一套规则
限制 发送和接收的顺序
限制 数据的格式
不限制 数据的内容
案例
Client <-> Server number -> <- number
常用协议种类
Request / Response
协议
客户端 知道 服务器 在哪
客户端 向 服务器 建立连接
客户端 发送数据给 服务器
服务器 返回数据给 客户端
注意
服务器不会主动给 客户端 发数据!!!
案例
SQL Query
Web Page
Subscription / Publish / Subpub
服务器会主动给客户端发消息
协议
客户端 告诉服务器 客户端的位置
服务器 知道 客户端 在哪
服务器 向 客户端 建立连接
服务器 发送数据给 客户端
案例
Notification
Keep Socket
协议
客户端 知道 服务器 在哪
客户端 向 服务器 建立连接
服务器 和 客户端 来回发来发去
案例
时时在线聊天
在线多人游戏
SRRP 协议
一个简单的 Request Response 协议
设计
简称
SRRP (自己拟定的一个名字)
协议
Client <-> Server action: String -> data: String -> <- status: String <- data: String

基础搭建
com/Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Server { public static void main(String[] args) throws Exception{ ServerSocket serverSocket = new ServerSocket(2440); while (true) { Socket socket = serverSocket.accept(); Connector connector = new Connector(socket); String action = connector.readLine(); String data = connector.readLine(); String status = "ok"; String result = "result"; connector.writeLine(status); connector.writeLine(result); } }}
com/Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 2440); Connector connector = new Connector(socket); String action = "action"; String data = "data"; connector.writeLine(action); connector.writeLine(data); String status = connector.readLine(); String result = connector.readLine(); System.out.println(status); System.out.println(result); }}
基于 SRRP 的计算器 的 API
基于 SRRP 继续制定约束
action : "add" data : "2,4" status : "ok" data : "6"
代码
com/Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class Server { public static void main(String[] args) throws Exception{ ServerSocket serverSocket = new ServerSocket(2440); while (true) { Socket socket = serverSocket.accept(); Connector connector = new Connector(socket); String action = connector.readLine(); String data = connector.readLine(); if (action.equals("add")) { String[] parts = data.split(","); double value1 = Double.parseDouble(parts[0]); double value2 = Double.parseDouble(parts[1]); double resultValue = value1 + value2; String status = "ok"; String result = resultValue + ""; connector.writeLine(status); connector.writeLine(result); } else { String status = "action not supported"; String result = ""; connector.writeLine(status); connector.writeLine(result); } } }}
com/Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 2440); Connector connector = new Connector(socket); String action = "sub"; double value1 = 4; double value2 = 6; String data = value1 + "," + value2; connector.writeLine(action); connector.writeLine(data); String status = connector.readLine(); String result = connector.readLine(); if (status.equals("ok")) { System.out.println(result); } else { System.out.println(status); } }}
网络数据传输
传输复杂的对象

基于 SRRP 的 对象传输
远程方法调用
相关名词
Remote Percedure Call