binarybittorrent

BitTorrent Peer wire protocol (TCP)


How are the messages encoded or sent/received by peers?

If there is a message

have: <len=0005><id=4><piece index> 

How is this sent(in binary,how is it translated to binary?) and received?

Is there a specific order in which the messages are sent to peers?

I have read the specification but it leaves me with questions.

Thanks


Solution

  • Prior to declaring the have message the specification says:

    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.

    You've got the binary format for length and id right there. The 'piece index' part is this message's specific payload. It should be four bytes long since the message has a fixed size of 5 bytes and 1 byte went to the message ID (viewing other messages with the same format should give you a clue).