lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Try LHF's lpack[1]. It reads from and writes to a [Lua] binary string. The syntax is very easy; in your case, it'd be something like:


--[ SERVER ]
require 'pack' -- or require 'lpack'

local s = string.pack('12d', 1,2,3,4,5,6,7,8,9,10,11,12)

socket:write(s)

--[ CLIENT ]

require 'pack'

-- Here you will read the buffer from the socket. Instead of trying to
-- read the number of bytes that 12 doubles use, you could, for instance
-- read a line or read until the connection closes.
local s = socket:read(12*SizeOfDoubleInYourMachine)

-- Convert the buffer to a table (an array) of doubles
local doubles = { string.unpack(s, '12d') }

-- Or make as many locals/globals as needed:
-- local d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12 = string.unpack(s, '12d')

for i,double in ipairs(doubles) do
	print(double)
end

Hope it helps you

--rb


[1] http://lua-users.org/wiki/LibrariesAndBindings . Search at the "Miscellaneous" section.