二进制协议与文本协议 [英] binary protocols v. text protocols

查看:16
本文介绍了二进制协议与文本协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人对什么是二进制协议有一个很好的定义?什么是文本协议?就在线发送的位而言,它们如何相互比较?

does anyone have a good definition for what a binary protocol is? and what is a text protocol actually? how do these compare to each other in terms of bits sent on the wire?

以下是维基百科关于二进制协议的描述:

here's what wikipedia says about binary protocols:

二进制协议是一种旨在或预期由机器而非人类读取的协议 (http://en.wikipedia.org/wiki/Binary_protocol)

A binary protocol is a protocol which is intended or expected to be read by a machine rather than a human being (http://en.wikipedia.org/wiki/Binary_protocol)

哦,来吧!

更清楚地说,如果我有 jpg 文件,它将如何通过二进制协议发送以及如何通过文本协议发送?当然,就在线路上发送的位/字节而言.

to be more clear, if I have jpg file how would that be sent through a binary protocol and how through a text one? in terms of bits/bytes sent on the wire of course.

归根结底,如果您查看一个字符串,它本身就是一个字节数组,因此这两种协议之间的区别应该取决于在线发送的实际数据.换句话说,关于初始数据(jpg 文件)在发送之前是如何编码的.

at the end of the day if you look at a string it is itself an array of bytes so the distinction between the 2 protocols should rest on what actual data is being sent on the wire. in other words, on how the initial data (jpg file) is encoded before being sent.

推荐答案

二进制协议与文本协议与二进制 blob 的编码方式无关.区别在于协议是面向数据结构还是面向文本字符串.让我举个例子:HTTP.HTTP 是一种文本协议,即使它发送 jpeg 图像时,它也只是发送原始字节,而不是它们的文本编码.

Binary protocol versus text protocol isn't really about how binary blobs are encoded. The difference is really whether the protocol is oriented around data structures or around text strings. Let me give an example: HTTP. HTTP is a text protocol, even though when it sends a jpeg image, it just sends the raw bytes, not a text encoding of them.

但是使 HTTP 成为文本协议的原因在于 get jpg 的交换看起来像这样:

But what makes HTTP a text protocol is that the exchange to get the jpg looks like this:

请求:

GET /files/image.jpg HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.01 [en] (Win95; I)
Host: hal.etc.com.au
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8

回复:

HTTP/1.1 200 OK
Date: Mon, 19 Jan 1998 03:52:51 GMT
Server: Apache/1.2.4
Last-Modified: Wed, 08 Oct 1997 04:15:24 GMT
ETag: "61a85-17c3-343b08dc"
Content-Length: 60830
Accept-Ranges: bytes
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: image/jpeg

<binary data goes here>

请注意,这很容易被更紧密地打包成一个看起来(在 C 中)类似的结构

Note that this could very easily have been packed much more tightly into a structure that would look (in C) something like

请求:

struct request {
  int requestType;
  int protocolVersion;
  char path[1024];
  char user_agent[1024];
  char host[1024];
  long int accept_bitmask;
  long int language_bitmask;
  long int charset_bitmask;
};

回复:

struct response {
  int responseType;
  int protocolVersion;
  time_t date;
  char host[1024];
  time_t modification_date;
  char etag[1024];
  size_t content_length;
  int keepalive_timeout;
  int keepalive_max;
  int connection_type;
  char content_type[1024];
  char data[];
};

字段名称根本不需要传输的地方,例如,响应结构中的 responseType 是一个值为 200 的 int 而不是三个字符 '2' '0''0'.这就是基于文本的协议:旨在作为(通常是人类可读的)文本行的平面流进行通信,而不是作为许多不同类型的结构化数据进行通信的协议.

Where the field names would not have to be transmitted at all, and where, for example, the responseType in the response structure is an int with the value 200 instead of three characters '2' '0' '0'. That's what a text based protocol is: one that is designed to be communicated as a flat stream of (usually human-readable) lines of text, rather than as structured data of many different types.

这篇关于二进制协议与文本协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆