As explained in another answer, bencoding is a binary format, not a text format. It seems you are trying to enter the message body in netcat using a terminal. Terminals are intended for entering text input into programs, not binary ones, and therefore you cannot directly enter this sequence into netcat stdin.
The identifier in your ping request should not be torrent-infohash. It must be a unique identifier to identify your customer on the DHT network. For testing, you can really just choose an identifier consisting of 20 ASCII characters and avoid these encoding problems, but in practice you will need uniform random binary identifiers.
If you want to use the binary identifier in the terminal, you should not try to enter it directly into netcat. Instead, you can use the shell echo command and hexadecimal encoding to get the data in the intended format and transfer it to netcat. For example, in bash:
echo -n $'d1:ad2:id20:\x23\x71\x0c\x1c\xb4\x50\x7d\x87\x29\xb8\x3f\x87\x2c\xc6\xa2\xa4\x4c\x39\x73\x67e1:q4:ping1:t1:01:y1:qe' | nc -u router.utorrent.com 6881
Please note that the answer you get from node will be unescaped binary, not necessarily text, so displaying it directly in your terminal as we do this may cause things to appear strange or your current the terminal session will be spoiled in some way.
source share