There is no transfer of binary data in udp-packages.

I use ESPLorer LUA send(port, ip, Buf)

The program runs on a UDP server, waits for a command to request data from a client, waits and sends data. This causes troubles:

(PANIC: unprotected error in call to Lua API (init.lua: 12: bad argument # 3 to 'send' (string expected, got table))

Passing string data is fine. But I need to transfer binary (measurement results). string Translation - do not offer

  • one
    "Translation in string - do not offer" - if the API expects the string type, then there's nothing to be done about it, just translate. Or use another API / library / write your own version. - zed
  • I don't know what Esplorer is, but obviously, the problem is in the inconsistency of your code with its API. Read the documentation more carefully or ask the author. Here, for example: github.com/4refr0nt/ESPlorer/issues - Beast Winterwolf

1 answer 1

You cannot send a lua table over the network. But binary data is string . So you need to make a message generator based on this table itself. And yet, before sending the message body, it is recommended to send its size, for example:

 function parsetable(t) local res="" -- Делаем что-то с таблицей return res end function numberToByteCode(x) local line="" local res1,res2=intdiv(x,256) if res1 > 255 or res1==0 then line=string.char(res2)..line elseif res1 > 0 then line=string.char(res1)..string.char(res2)..line end while res1 > 0 do res1,res2=intdiv(res1,256) if res1 > 255 then line=string.char(res2)..line elseif res1 > 0 then line=string.char(res1)..string.char(res2)..line end end return line end function byteCodeToNumber(x) local y=0 local z=0 for n=#x,1,-1 do if z==0 then y=y+(x:sub(n,n):byte()) else y=y+(x:sub(n,n):byte())*256^z end z=z+1 end return y end function fromNumber32(n) local bytecode=numberToByteCode(n) if #bytecode > 4 then bytecode="\255\255\255\255" elseif #bytecode < 4 then bytecode=string.rep("\0",4-#bytecode)..bytecode end end function format(msg) return fromNumber32(#msg)..msg end function get() local size=byteCodeToNumber(receive(4)) return receive(size) end function sendparsed(ip,port,t) send(ip,port,format(parsetable(t))) end 

Thus, the maximum message length will be ~ 4 GB, and special characters will not interfere with message transmission.