两句话:
协议中的所有剩余消息均采用<长度前缀><消息ID><有效负载>
的形式。 长度前缀是一个四字节大端值。 消息 ID 是一个十进制字节。 有效负载取决于消息。
请求:
请求消息是固定长度的,用于请求一个块。
有效负载包含以下信息:
- index:指定从零开始的片段索引的整数
- begin:指定片段内从零开始的字节偏移量的整数
- length:指定请求长度的整数。
当我写下所有内容时,它总共有 5 个字节。 使用
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write( 13 );
byteStream.write( 6 );
byteStream.write( index );
byteStream.write( begin );
byteStream.write( length );
message = byteStream.toByteArray();
编辑:抱歉,当我写它时,我有点生气。 它是比特流协议。
使用此规范。
Two quotes:
All of the remaining messages in the protocol take the form of <length prefix><message ID><payload>
. The length prefix is a four byte big-endian value. The message ID is a single decimal byte. The payload is message dependent.
request: <len=0013><id=6><index><begin><length>
The request message is fixed length, and is used to request a block.
The payload contains the following information:
- index: integer specifying the zero-based piece index
- begin: integer specifying the zero-based byte offset within the piece
- length: integer specifying the requested length.
When I write everything it sums up to 5 bytes. Using
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write( 13 );
byteStream.write( 6 );
byteStream.write( index );
byteStream.write( begin );
byteStream.write( length );
message = byteStream.toByteArray();
EDIT: Sorry i was kind of pissed when i wrote it. its the bittorent protocol.
Using this spec.
发布评论
评论(4)
write() 方法写入一个字节。
如果你向它发送一个 char 或 int,它只会用 & 删除第 8 位以上的所有内容。 0xFF。
DataOutputStream 有更多选项(writeInt、writeShort 等),但它使用大端字节顺序,因此您可能需要在将值传递给 writeXYZ( 之前执行 Integer.reverseBytes() (或 Short.reverseBytes())调用) 方法。
备注:规范中没有规定
index
、begin
和length
的长度。 我只是想提供可用选项的示例。编辑2:根据 D.Shawley 的答案和找到的规范编辑示例此处。
The write() method writes one byte.
If you send it a char or int it just strips everything above the 8th bit with & 0xFF.
You have more options with DataOutputStream (writeInt, writeShort etc.) but it uses big endian byte order so you might need to perform an Integer.reverseBytes() (or Short.reverseBytes()) call before passing in the value to the writeXYZ() method.
Remark: The spec doesn't state the length of
index
,begin
andlength
. I just wanted to give a sample of the available options.Edit 2: Edited the sample based on D.Shawley's answer and a spec found here.
我不确定你在这里得到什么...引用的文本没有说明
、
或
是。 第一个引用相当清楚地指出,消息由 4 字节长度、后跟 1 字节标识符和任意有效负载组成。有效负载的长度可能是
指定的值或
+5,具体取决于
代码> 的意思。 第二个引号看起来像是由 1 字节标识符 0x06 标识的任何消息的定义。 我猜测:
的字节,可能有 14 个字节长,无论如何,您生成的字节流似乎与消息定义不匹配并且消息定义缺乏清晰度。
I'm not sure what you are getting at here... the quoted text doesn't say what the length of
<index>
,<begin>
, or<length>
is. The first quote states, rather clearly, that a message consists of a 4-byte length, followed by a 1-byte identifier, and an arbitrary payload.The length of the payload is probably either the value specified as
<length>
or<length>
+5 depending on exactly what<length>
means. The second quote looks like the definition of whatever message is identified by the 1-byte identifier of 0x06. I would guess that:<index><begin><length>
, is probably 14 bytes longIn any case, the byte stream that you are generating does not seem to match the message definition AND the message definition lacks clarity.
write() 写入字节。 5 个 write() 产生 5 个字节。
write() writes bytes. 5 write()'s produces 5 bytes.
请参阅
写(int b)
。See
write(int b)
.