Java网络:协议设计 Protocol Design

本文翻译转载:原文 Java网络:协议设计

如果您正在设计客户端 - 服务器系统,则可能还必须在客户端和服务器之间设计通信协议。当然,这个协议已有现成为你选择,例如HTTP,XML-RPC(基于HTTP的XML)或SOAP(也是基于HTTP的XML)。但是协议是可以自定义的,所以让我们看一下在设计  客户端-服务器协议  时可能想要考虑的一些问题 -

  1. 客户端 - 服务器往返
  2. 划分请求和响应的结束
  3. 渗透防火墙


客户端 - 服务器往返

当客户端和服务器通信以执行某些操作时,它们会交换信息。例如,客户端将要求执行服务,服务器将尝试执行该服务,并发回一个响应,告知客户端结果。客户端和服务器之间的这种信息交换称为往返 

当计算机(客户端或服务器)通过互联网向另一台计算机发送数据时,从发送数据到另一端接收数据需要一些时间。这是数据通过互联网传输的时间。这个时间称为延迟

协议中往返越多,协议就越慢,特别是在延迟很高的情况下。HTTP协议仅包含单个请求和单个响应以执行其服务。换句话说,单次往返。另一方面,SMTP协议包括在发送电子邮件之前客户端和服务器之间的几次往返。

如果您有大量数据要从客户端发送到服务器,那么将协议分解为多次往返的唯一原因是。在这种情况下,您有两种选择:

  1. 在单独的往返中发送标题信息。
  2. 将邮件正文拆分为较小的块。

如果服务器可以对例如标题信息进行一些初始预验证,则在单独的往返(第一个)中发送标题可以是智能的。如果该标题信息无效,那么发送大量数据本来就是一种浪费。

如果在传输大量数据时网络连接失败,则可能必须从头开始重新发送所有数据。通过将数据分成更小的块,您只需从网络连接失败的块中重新发送块。成功转移的块不会被重新发送。

划分请求和响应的结束

如果您的协议允许通过同一连接发送多个请求,您需要一些方法让服务器知道一个请求何时结束,并且新的开始。客户端还需要知道一个响应何时结束,另一个响应何时开始。

您可以通过两种方式划分请求的结尾:

  1. 在请求开始时发送请求的长度(以字节为单位)。
  2. 在请求数据之后发送请求结束标记。

HTTP使用第一种机制。在其中一个请求标头中,发送“Content-Length”。此标头指示属于请求的标头之后的字节数。下面是一个常规post 的请求内容,head之后空一行就是body

POST /WebServerTest_war_exploded/HelloWorld HTTP/1.1 //必须
Host: 10.10.14.235:8080 //必须
Content-Length: 23 //body 长度
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Accept-Encoding: gzip
Cookie: key=value; key2=value2
User-Agent: myhttp/3.10.0

key1=value1&key2=value2 //body

此模型的优点是您没有请求结束标记的开销。您也不必编码数据正文以避免数据看起来像请求结束标记。

第一种方法的缺点是发送方必须知道在传输数据之前传输了多少字节。如果数据是动态生成的,则首先必须在发送数据之前缓冲所有数据,以计算字节数。

通过使用请求结束标记,您不必知道要发送的字节数。您只需要在数据末尾发送请求结束标记。但是,您必须确保发送的数据不包含任何可能被误认为请求结束标记的数据。这是一种方法:

让我们假设请求结束标记是字节值255.当然数据也可以包含值255。因此,对于包含值255的数据中的每个字节,您添加一个额外的字节,也是值255,则数据变为 255,255。 请求结束标记从字节值255更改为 255,0 。这是编码总结:

255 in data  - > 255,255 
end-of-request  - > 255,0

数据中永远不会出现序列255,0,因为您将所有255更改为255,255。并且,255,255,0不会被误认为是255,0。服务器解析时,前面255将被解释,最后的0被解释。

渗透防火墙

大多数防火墙阻止除HTTP协议之外的所有其他流量。因此,将协议层叠在HTTP上是一个好主意,例如XML-RPC,SOAP和REST。

要将协议分层在HTTP上,您可以在HTTP请求和响应中的客户端和服务器之间反复发送数据。请记住,HTTP请求和响应不仅包含文本或HTML。您也可以在其中发送二进制数据。

通过在HTTP协议上分层请求,唯一可能有点奇怪的是HTTP请求必须包含 “Host” 头字段。如果您在HTTP上设计P2P协议,您的同伴很可能不会运行多个“主机”。这种必需的头字段在那种情况下是不必要的开销(但是很小的开销)。

这段是说如果你自己想扩展http的话,其中请求行和head中的host是必须的,上面注释也写了,请注意。

 

我觉得这篇写的非常好,翻译后写了点自己的理解,如有侵权,请告知删除。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book introduces the reader to the principles used in the construction of a large range of modern data communication protocols. The approach we take is rather a formal one, primarily based on descriptions of protocols in the notation of CSP. This not only enables us to describe protocols in a concise manner, but also to reason about many of their interesting properties and formally to prove certain aspects of their correctness with respect to appropriate specifications. Only after considering the main principles do we go on to consider actual protocols where these principles are exploited. This is a completely new edition of a book which was first published in 1994, where the main focus of many international efforts to develop data communication systems was on OSI – Open Systems Interconnection – the standardised architecture for communication systems developed within the International Organisation for Standardization, ISO. In the intervening 13 years, many of the specific protocols developed as part of the OSI initiative have fallen into disuse. However, the terms and concepts introduced in the OSI Reference Model are still essential for a systematic and consistent analysis of data communication systems, and OSI terms are therefore used throughout. There are three significant changes in this second edition of the book which particularly reflect recent developments in computer networks and distributed systems. Firstly, the rise of the Internet has led to the development of large numbers of new protocols, particularly in the Application Layer, and a new chapter has been introduced focussing on some of the most important of these. These new protocols typically use new styles of encoding, particularly ASCII encodings such as MIME and XML, and rely on middleware solutions based on a variety of support protocols. Chapters 8 and 10 have therefore been extended to deal with these topics. Finally, there is today much more focus on security issues in networks. Chapter 6,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值