About the pack problem in PHP, how can I encode and decode DHT's incoming stream?

Source: Internet
Author: User
Tags unpack
I try to use PHP to do a DHT crawler, because the transmission of data using Bencode, this is not a problem, can be correctly encoded decoding, but if you send find_node data, the data obtained there is a nodes, This is a binary stream encoded data using a struct structure, according to the DHT protocol, is: 20 bytes of Node ID + 4 bytes of IP + 2 bytes of port, I have seen in Python encoding decoding method, but can not be converted to PHP, in Python this translates:

def decodenodes (nodes): "" "Turn the    received nodes to the precedent table.     Data format: [(Node ID, IP, port), (node ID, IP, port), (node ID, IP, port) ....]    "" " n = []    nrnodes = len (nodes)/    nodes = Unpack ("!" + "20sIH" * nrnodes, nodes) for    i in Xrange (nrnodes):        Nid, IP, port = nodes[i * 3], Numtodottedquad (nodes[i * 3 + 1]), Nodes[i * 3 + 2]print nid        N.append (NID, IP, port) 
  return ndef Encodenodes (nodes): "" "" "    Encode" nodes with Decodenodes    "" "    n = [] for    node in nodes :        n.extend ([Node.nid, Dottedquadtonum (NODE.IP), Node.port])    return Pack ("!" + "20sIH" * len (nodes), *n)


How can I change this in PHP?


Reply to discussion (solution)

20 bytes of node id:a20 or A20 if it is filled with blanks, use the latter
4-byte ip:l IP address converted to unsigned long-shaped number with Ip2long
2-byte port: S

Packaged
$s = pack (' A20ls ', $nodeid, Ip2logn ($IP), 456);
Unpack
$d = Unpack (' A20nodeid/lip/sport ', $s);
$d [' ip '] = logn2ip ($d [' IP ']);
Print_r ($d); It's clear.

20 bytes of node id:a20 or A20 if it is filled with blanks, use the latter
4-byte ip:l IP address converted to unsigned long-shaped number with Ip2long
2-byte port: S

Packaged
$s = pack (' A20ls ', $nodeid, Ip2logn ($IP), 456);
Unpack
$d = Unpack (' A20nodeid/lip/sport ', $s);
$d [' ip '] = logn2ip ($d [' IP ']);
Print_r ($d); It's clear.



Thank you very much, it's settled.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.