Inferno
Inferno

Reputation: 47

Erlang get string from UDP packet

I have this udp packet:

P = <<83,65,77,80,188,64,172,85,30,144,105,0,0,0,50,0,7,0,0,0,115,97,109,112,45,114,112,11,0,0,0,149,78,87,149,82,80,149,118,50,46,50,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>

14-15 byte is the players var (Byte width - 2) 15-18 byte of it is the length of the server hostname (Byte width - 4)
19 + strlen is the hostname of server (Byte width - strlen)

I get players var so:

<<_:11/bytes, Players:16/integer-big, Max:16/integer-big, _/binary>> = P.

It's 50.
How can I get the hostname?

Upvotes: 2

Views: 240

Answers (1)

Diego Sevilla
Diego Sevilla

Reputation: 29001

You can improve the expression to obtain the correct values. Note that server length, as you put it, is 32 bits, and, by the look of it, it seems that it is little endian, not big endian (note how the name is 7 bytes, in this case "samp-rp", and the coding of these bytes is <<7,0,0,0>>, which indicates little endian (maybe your players are also little endian). Also, your numbers seem a little bit off. The expression would then be:

<<_:14/bytes, Players:16/integer-little, HNameLength:32/integer-little, HostNameBinary:HNameLength/binary, _/binary>> = P.

Then, the host name can be converted to a string from the binary with binary_to_list:

HostName = binary_to_list(HostNameBinary).

Upvotes: 3

Related Questions