1

我收到一个 UDP 数据包,如下所示:

<<83,65,77,80,188,64,171,138,30,120,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>

如果我知道我可以删除前 11 个字节,并且 12-13 字节包含服务器上在线玩家的数量(字节宽度为 2),我该如何解码数据包,我怎样才能得到这个数量?

UPD
也许我发送了不正确的数据包...
SAMP 查询
所以,我发送:

<<$S,$A,$M,$P,188,64,172,136,7808:16,$i>>

对于服务器 188.64.172.136:7808,我得到

<<83,65,77,80,188,64,172,136,30,128,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>
4

2 回答 2

2

您可以使用位语法和巧妙的模式匹配来获得结果:

<<_:11/bytes, NumberOfPlayers:16/integer-big, _/binary>> = <<83,65,77,80,188,64,171,138,30,120,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>,
NumberOfPlayers.
于 2012-06-28T10:12:08.283 回答
1

如果您的数据包二进制文件存储在 中P,您可以执行以下操作(假设大端):

<<NumberOfPlayersOnline:16/big>> = binary:part(P,11,2).

结果在NumberOfPlayers.

于 2012-06-28T10:07:19.430 回答